-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
38 lines (30 loc) · 873 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# coding=utf-8
import pandas as pd
from collections import Counter
class PDtable:
"""
.add(data, "colname")
.to_pandas
"""
def __init__(self):
self.column_datas = {}
self.column_names = []
def add(self, data, colname):
if colname not in self.column_datas:
self.column_datas[colname] = []
self.column_names.append(colname)
self.column_datas[colname].append(data)
def to_pandas(self):
return pd.DataFrame(self.column_datas)[self.column_names]
def Ratio(vlist, nan=False):
cnt = Counter(vlist)
if not nan:
total = float(len([t for t in vlist if not pd.isnull(t)]))
else:
total = float(len(vlist))
ans = {}
for tk in cnt:
if not nan and pd.isnull(tk):
continue
ans[tk] = cnt[tk] / total
return ans