问题
I have a dataframe test
with 3 columns id, name, value
the following column test['values']
. A sample of how test
looks is:
name values
0 impressions [{'value': 17686, 'end_time': '2018-06-12T07:0...
1 reach [{'value': 6294, 'end_time': '2018-06-12T07:00...
2 follower_count [{'value': 130, 'end_time': '2018-06-12T07:00:...
3 email_contacts [{'value': 1, 'end_time': '2018-06-12T07:00:00...
4 phone_call_clicks [{'value': 0, 'end_time': '2018-06-12T07:00:00...
5 text_message_clicks [{'value': 0, 'end_time': '2018-06-12T07:00:00...
6 get_directions_clicks [{'value': 0, 'end_time': '2018-06
The test value cells look something like this:
[{'end_time': '2018-06-12T07:00:00+0000', 'value': 17686},
{'end_time': '2018-06-13T07:00:00+0000', 'value': 4064}]
I can expand it by doing the following:
test[['Values 1', 'Values 2']] = test['values'].apply(pd.Series)
test[['Date 1', 'Values 1']] = test['Values 1'].apply(pd.Series)
test[['Date 2', 'Values 2']] = test['Values 2'].apply(pd.Series)
test.drop(['values'], axis=1, inplace=True)
The result is something like this:
id name Values 1 Values 2 Date 1 Date 2
/insights/impressions/day impressions 17686 4064 2018-06-12T07:00:00+0000 2018-06-13T07:00:00+0000
/insights/reach/day reach 6294 2085 2018-06-12T07:00:00+0000 2018-06-13T07:00:00+0000
/insights/follower_count/day follower_count 130 37 2018-06-12T07:00:00+0000 2018-06-13T07:00:00+0000
I was wondering if:
a. There's a faster way to expand the list of dictionaries
b. There's a way to unpivot the data so that values 1 and values 2 are on one column. And Date 1 and Date 2 are in another column
回答1:
If input data are jsons, better is use json_normalize.
j = [{'description': 'Total number 1', 'id': 'a', 'name': 'impressions', 'period': 'day', 'title': 'Impressions', 'values': [{'end_time': '2018-06-12T07:00:00+0000', 'value': 17686}, {'end_time': '2018-06-13T07:00:00+0000', 'value': 4064}]},
{'description': 'fn', 'id': 'b', 'name': 'impressions', 'period': 'day', 'title': 'Impressions', 'values': [{'end_time': '2018-06-12T07:00:00+0000', 'value': 17686}, {'end_time': '2018-06-13T07:00:00+0000', 'value': 4064}]}]
from pandas.io.json import json_normalize
df = json_normalize(j, 'values')
print (df)
end_time value
0 2018-06-12T07:00:00+0000 17686
1 2018-06-13T07:00:00+0000 4064
2 2018-06-12T07:00:00+0000 17686
3 2018-06-13T07:00:00+0000 4064
But if need also add original columns:
from pandas.io.json import json_normalize
df = json_normalize(j, 'values', ['description', 'id', 'name', 'period', 'title'])
print (df)
end_time value description id name period \
0 2018-06-12T07:00:00+0000 17686 Total number 1 a impressions day
1 2018-06-13T07:00:00+0000 4064 Total number 1 a impressions day
2 2018-06-12T07:00:00+0000 17686 fn b impressions day
3 2018-06-13T07:00:00+0000 4064 fn b impressions day
title
0 Impressions
1 Impressions
2 Impressions
3 Impressions
First solution:
test = pd.DataFrame({
'name':['a', 'b', 'n'],
'values':[[{'end_time': '2018-06-12T07:00:00+0000', 'value': 17686},
{'end_time': '2018-06-13T07:00:00+0000', 'value': 4064}],[{'end_time': '2018-06-12T07:00:00+0000', 'value': 17686},
{'end_time': '2018-06-13T07:00:00+0000', 'value': 4064}],[{'end_time': '2018-06-12T07:00:00+0000', 'value': 17686},
{'end_time': '2018-06-13T07:00:00+0000', 'value': 4064}]]
})
df = (pd.concat([pd.DataFrame(x) for x in test['values']], axis=1, keys=(1, 2))
.stack(0)
.reset_index(level=1, drop=True))
print (df)
end_time value
0 2018-06-12T07:00:00+0000 17686
0 2018-06-12T07:00:00+0000 17686
1 2018-06-13T07:00:00+0000 4064
1 2018-06-13T07:00:00+0000 4064
df = test.join(df)
print (df)
name values \
0 a [{'end_time': '2018-06-12T07:00:00+0000', 'val...
0 a [{'end_time': '2018-06-12T07:00:00+0000', 'val...
1 b [{'end_time': '2018-06-12T07:00:00+0000', 'val...
1 b [{'end_time': '2018-06-12T07:00:00+0000', 'val...
2 n [{'end_time': '2018-06-12T07:00:00+0000', 'val...
end_time value
0 2018-06-12T07:00:00+0000 17686.0
0 2018-06-12T07:00:00+0000 17686.0
1 2018-06-13T07:00:00+0000 4064.0
1 2018-06-13T07:00:00+0000 4064.0
2 NaN NaN
回答2:
You can create both column value and end_time at once with two apply
and stack
(plus set_index
, reset_index
):
(test.set_index('name')['values']
.apply(pd.Series).stack()
.apply(pd.Series).reset_index().drop('level_1',1))
the output is like this:
name end_time value
0 impressions 2018-06-12T07:00:00+0000 17686
1 impressions 2018-06-13T07:00:00+0000 4064
来源:https://stackoverflow.com/questions/50839737/pandas-column-expansion-of-list-of-dictionary-how-to-optimise