[英]how to split a nested dictionary inside a column of a dataframe into new rows?
I have a dataframe:我有一个 dataframe:
Col1 Col2 Col3
01 ABC {'link':'http://smthing1}
02 DEF {'link':'http://smthing2}
I need to split col3 into new rows: expected output dataframe:我需要将 col3 拆分为新行:预期 output dataframe:
Col1 Col2 Col3
01 ABC 'http://smthing1'
02 DEF 'http://smthing2'
This doesnt seem to work:这似乎不起作用:
df= df.apply(pd.Series)
Use Series.str.get
, but first convert to dictionaries if necessary:使用
Series.str.get
,但如有必要,首先转换为字典:
#converting to dicts
#import ast
#df['Col3'] = df['Col3'].apply(ast.literal_eval)
df['Col3'] = df['Col3'].str.get('link')
If you like long code:如果你喜欢长代码:
import pandas as pd
my_list = [[1, 'ABC', {'link':'http://smthing1'}], # list
[2, 'DEF', {'link':'http://smthing2'}]]
df = pd.DataFrame(my_list, columns=['col1', 'col2', 'col3']) #convert to pandas dataframe
convert_col3_to_dict = dict(df['col3']) #convert string to dict
list1 = []
list2 = []
for i in range(len(my_list)):
elements = my_list[i][:2]
#remove empty list
elements = [x for x in elements if x != []]
# print(elements)
list1.append(elements)
for key, val in convert_col3_to_dict.items(): #Iterating through the col3
for k,v in val.items(): #iterating through values in col3
# print([v]) # printing the value
list2.append([v])
new_list = []
for f, b in zip(list1, list2):
# print(f, b)
tem_list = f + b
new_list.append(tem_list)
new_df = pd.DataFrame(new_list, columns=['col1', 'clo2', 'col3'])
print(new_df)
Result:结果:
col1 clo2 col3
0 1 ABC http://smthing1
1 2 DEF http://smthing2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.