我有一个这样的Pandas数据框(这是Twitter句柄的列表):
label favees
0 1NewsNZ 1
1 mhjb 1
2 citizenai_nz 1
...
我想用相关Twitter资料中的一些信息充实它。 python-twitter UsersLookup函数返回如下字典列表:
{"created_at": "Fri Apr 04 09:03:48 +0000 2008", "description": "Stay ahead with 1 NEWS
| Instagram and Snapchat: 1NewsNZ", "favourites_count": 2447, "followers_count": 152214,
"friends_count": 239, ...}
我想做的是一次将所有100行的概要文件中的那三列中的值都复制到块数据帧中。下面的最后三行将不起作用,但可能会给出我要执行的操作的想法:
def populate_profiles(people_csv):
people = pd.read_csv(OUTPUT_FOLDER + people_csv)
api = connect_to_twitter.api()
people_chunks = df_chunks(people, 100)
for chunk in people_chunks:
profiles = api.UsersLookup(screen_name=chunk['label'].values.tolist())
chunk['name'] = profiles['name']
chunk['description'] = profiles['description']
chunk['image'] = profiles['profile_image_url']
之后,我将充实的行添加到新的CSV中。 (df_chunks
来自https://stackoverflow.com/a/44729807/1876628)
我觉得答案与dataframe.map有关。
答案 0 :(得分:0)
不确定这是最好的方法,但这似乎可行:
chunk['name'] = [profile.name for profile in profiles]
chunk['description'] = [profile.description for profile in
chunk['image'] = [profile.profile_image_url for profile in profiles]
感谢this answer所需的线索。
如果有更优雅的方式请唱歌…