对dataframe的字段的处理
怎么把这个keywords列里面的name 提取出来转成x|x|x的格式。
import pandas as pd
dic1={'id':['862','8844','7890'],'keywords':[[{'id':931,'name':'jealousy'},{'id':932,'name':'jealousy'}],[{'id':931,'name':'jealousy'},{'id':932,'name':'jealousy'}],[{'id':931,'name':'jealousy'},{'id':932,'name':'jealousy'}]]}
df=pd.DataFrame(dic1)
print(df)
for index, row in df.iterrows():
str1 = row['keywords'][0]['name']
for i in range(1, len(row['keywords'])):
str1 = str1 + '|' + row['keywords'][i]['name']
row['keywords'] = str1
print(df)
运行结果
id keywords
0 862 [{'id': 931, 'name': 'jealousy'}, {'id': 932, ...
1 8844 [{'id': 931, 'name': 'jealousy'}, {'id': 932, ...
2 7890 [{'id': 931, 'name': 'jealousy'}, {'id': 932, ...
id keywords
0 862 jealousy|jealousy
1 8844 jealousy|jealousy
2 7890 jealousy|jealousy
若有帮助,谢谢采纳~