我从推特上收集了数据。我有很多专栏,但其中一个有问题:
tweets_data['Mentioned Users']
0 [{'username': 'HuntTerrorist', 'displayname': ...
1 [{'username': 'AttorneyCrump', 'displayname': ...
2 None
3 [{'username': 'realDonaldTrump', 'displayname'...
4 None
...
19995 None
19996 None
19997 None
19998 None
19999 None
Name: Mentioned Users, Length: 20000, dtype: object
我想从每一行中获取用户名,并将它们放回数据帧中
我已尝试使用“爆炸”命令:
exploded_df = tweets_data.explode('Mentioned Users')
user_df = exploded_df['Mentioned Users'].apply(pd.Series)
user_df['username']
0 HuntTerrorist
0 stinkytcat1
0 Nexussfire
0 BBCWorld
1 AttorneyCrump
...
19995 NaN
19996 NaN
19997 NaN
19998 NaN
19999 NaN
Name: username, Length: 24886, dtype: object
这段代码的问题是,它将每个提到的用户打印在不同的行中,从而扩展了总行数。由于长度不同,我无法将此列添加到原始列。因此,我希望索引为0的所有用户名都在同一行中(因为它们都在同一条tweet中)
是否有任何方法可以组合用户名或任何替代方法来实现我的目标
与其
exploding
,不如直接从字典中为所需的列编制索引,如下所示相关问题 更多 >
编程相关推荐