-
Notifications
You must be signed in to change notification settings - Fork 21
/
pd.py
44 lines (29 loc) · 835 Bytes
/
pd.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
'''Utilities for Pandas dataframe
This file is a part of BdPy
'''
__all__ = ['convert_dataframe', 'append_dataframe']
import pandas as pd
def convert_dataframe(lst):
'''Convert `lst` to Pandas dataframe
Parameters
----------
lst : list of dicts
Returns
-------
Pandas dataframe
'''
df_lst = (pd.DataFrame([item.values()], columns=item.keys()) for item in lst)
df = pd.concat(df_lst, axis=0, ignore_index=True)
return df
def append_dataframe(df, **kwargs):
'''Append a row to Pandas dataframe `df`
Parameters
----------
df : Pandas dataframe
kwargs : key-value of data to be added in `df`
Returns
-------
Pandas dataframe
'''
df_append = pd.DataFrame({k : [kwargs[k]] for k in kwargs})
return df.append(df_append, ignore_index=True)