df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
应该给你结果
c = ['customer_id','rating']
d = [[44224,{'overall': 5, 'description': 3}],
[55243,{'overall': 3, 'description': 2}]]
import pandas as pd
df = pd.DataFrame(d,columns=c)
print (df)
df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
print (df)
这个的输出是:
原始数据框:
customer_id rating
0 44224 {'overall': 5, 'description': 3}
1 55243 {'overall': 3, 'description': 2}
更新的数据框:
customer_id rating overall_rating
0 44224 {'overall': 5, 'description': 3} 5
1 55243 {'overall': 3, 'description': 2} 3
或者你可以给:
df['overall_rating'] = pd.DataFrame([x for x in df['rating']])['overall']
这个的输出也将是相同的:
c = ['customer_id','rating']
d = [[44224,{'overall': 5, 'description': 3}],
[55243,{'overall': 3, 'description': 2}]]
import pandas as pd
df = pd.DataFrame(d,columns=c)
print (df)
df['overall_rating'] = pd.DataFrame([x for x in df['rating']])['overall']
#df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
print (df)
原始数据框:
customer_id rating
0 44224 {'overall': 5, 'description': 3}
1 55243 {'overall': 3, 'description': 2}
更新的数据框:
customer_id rating overall_rating
0 44224 {'overall': 5, 'description': 3} 5
1 55243 {'overall': 3, 'description': 2} 3
具有浮点值的字典和没有“整体”条目的字典的示例
c = ['customer_id','rating']
d = [[44224,{'overall': 5, 'description': 3}],
[55243,{'overall': 3, 'description': 2}],
[11223,{'overall': 1.5, 'description': 2}],
[12345,{'description':3}]]
import pandas as pd
df = pd.DataFrame(d,columns=c)
print (df)
df['overall_rating'] = df['rating'].apply(lambda x: x.get('overall'))
print (df)
这个的输出是:
输入数据框
customer_id rating
0 44224 {'overall': 5, 'description': 3}
1 55243 {'overall': 3, 'description': 2}
2 11223 {'overall': 1.5, 'description': 2}
3 12345 {'description': 3}
更新后的 DataFrame 是:
customer_id rating overall_rating
0 44224 {'overall': 5, 'description': 3} 5.0
1 55243 {'overall': 3, 'description': 2} 3.0
2 11223 {'overall': 1.5, 'description': 2} 1.5
3 12345 {'description': 3} NaN