-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_data_convert.py
48 lines (40 loc) · 1.07 KB
/
utils_data_convert.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
39
40
41
42
43
44
45
46
47
48
import pandas
import pandas as pd
def make_list(s):
if type(s) == list:
return s
else:
return [s]
def from_df_to_list_of_dict(l1, l2=None):
"""
Transforming dataframe into list of dict (if necessary)
"""
if type(l1) == pandas.core.frame.DataFrame:
l1 = l1.to_dict('records')
if l2 is None:
return l1
if type(l2) == pandas.core.frame.DataFrame:
l2 = l2.to_dict('records')
return l1, l2
def from_df_to_numpy(df1, df2=None):
"""
Transforming dataframe into list of dict (if necessary)
"""
if type(df1) == pandas.core.frame.DataFrame:
df1 = df1.to_numpy()
if df2 is None:
return df1
if type(df2) == pandas.core.frame.DataFrame:
df2 = df2.to_numpy()
return df1, df2
def from_list_of_dict_to_df(l1, l2=None):
"""
Transforming list of dict into dataframe (if necessary)
"""
if type(l1) == list:
l1 = pd.DataFrame(l1)
if l2 is None:
return l1
if type(l2) == list:
l2 = pd.DataFrame(l2)
return l1, l2