Creating a list of dicts from Pandas df
Date : March 29 2020, 07:55 AM
With these it helps You can use iterrows for that. This lets you iterate over the rows as Series, not dicts, but that is pretty similar (e.g. has iteritems(), __getitem__, etc). If you must have use dicts, you can easily convert each Series to dict, using the to_dict() method. list_of_dicts = list( row.to_dict() for key, row in df.iterrows() )
|
Converting a string with list of dicts to pandas df
Tag : python , By : littlefuzz
Date : March 29 2020, 07:55 AM
it should still fix some issue I am reading a string from a file which looks like: , try this: In [73]: pd.DataFrame.from_dict([{'key1':'val1','key2':'val2'},{'key1':'val1','key2':'val2'}])
Out[73]:
key1 key2
0 val1 val2
1 val1 val2
import json
In [81]: s
Out[81]: '[{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]'
In [82]: pd.DataFrame.from_dict(json.loads(s))
Out[82]:
key1 key2
0 val1 val2
1 val1 val2
|
Search a list of strings in pandas dataframe and add each search string to a new column
Tag : pandas , By : user176445
Date : March 29 2020, 07:55 AM
around this issue I think you need findall: With sample data of @AndreyF: search = ['FR-001', 'FR-002', 'FR-003', 'FR-004']
df['FR'] = df['Description'].str.findall('(' + '|'.join(search) + ')')
print (df)
Description FR
0 AasfasfFR-001,asfasdfafsagsdg FR-002 [FR-001, FR-002]
1 AasfasfFR-004, FR-002 [FR-004, FR-002]
2 AasfasfFR-02,asfasdfafsagsdg []
3 AasfasfFR-001,asfasdfafsagsdg FR-003 [FR-001, FR-003]
4 AasfasfFR-004,asfasdfafsagsdg FR-002 [FR-004, FR-002]
df = df[df['FR'].astype(bool)]
print (df)
Description FR
0 AasfasfFR-001,asfasdfafsagsdg FR-002 [FR-001, FR-002]
1 AasfasfFR-004, FR-002 [FR-004, FR-002]
3 AasfasfFR-001,asfasdfafsagsdg FR-003 [FR-001, FR-003]
4 AasfasfFR-004,asfasdfafsagsdg FR-002 [FR-004, FR-002]
|
Pandas - Create df from list of dicts
Tag : python , By : user134570
Date : March 29 2020, 07:55 AM
it helps some times I have data in the following format (list of dicts that each contain a list of 3 lists): data=[{40258: [['2018-07-03T14:13:41'], ['Open'], ['Closed']]},
{40257: [['2018-07-03T13:47:55',
'2018-07-03T14:21:52',
'2018-07-04T11:56:44'],
['Open', 'In Progress', 'Waiting on 3rd Party'],
['In Progress', 'Waiting on 3rd Party', 'In Progress']]},
{40255: [['2018-07-03T13:12:58'], ['Open'], ['Closed']]},
{40250: [[], [], []]}]
f = lambda x: x + [np.nan]*(3-len(x))
mod_data = [ [k]+ sum(list(map(f, v)), []) for d in data for k,v in d.items()]
cols = ['key', 'List1-1', 'List1-2', 'List1-3', 'List2-1', 'List2-2', 'List2-3', 'List3-1', 'List3-2', 'List3-3']
df = pd.DataFrame(mod_data, columns=cols).set_index('key')
print(df)
List1-1 List1-2 List1-3 List2-1 List2-2 List2-3 List3-1 List3-2 List3-3
key
40258 2018-07-03T14:13:41 NaN NaN Open NaN NaN Closed NaN NaN
40257 2018-07-03T13:47:55 2018-07-03T14:21:52 2018-07-04T11:56:44 Open In Progress Waiting on 3rd Party In Progress Waiting on 3rd Party In Progress
40255 2018-07-03T13:12:58 NaN NaN Open NaN NaN Closed NaN NaN
40250 NaN NaN NaN NaN NaN NaN NaN NaN NaN
|
Python / Pandas - put a list of dicts into a Pandas DataFrame - Dict Keys schould be the columns
Date : March 29 2020, 07:55 AM
around this issue The pd.DataFrame constructor accepts a list of dictionaries directly. This will be more efficient than appending repeatedly to an existing dataframe. Here's a demo: d1 = {'name': 'Demetrius', 'number': '0001',
'style': 'D', 'text': 'Demetrius an der...',
'year': '1797'}
d2 = {'name': 'ABC', 'number': '0002',
'style': 'E', 'text': 'Some text',
'year': '1850'}
L = [d1, d2]
df = pd.DataFrame(L)
print(df)
name number style text year
0 Demetrius 0001 D Demetrius an der... 1797
1 ABC 0002 E Some text 1850
|