I have got the data from twitter in JSON format with multiple column. I am working with one of them and trying to get username of mentionedUsers and get them into a separete column.
print(tweets_data['mentionedUsers'])
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: mentionedUsers, Length: 20000, dtype: object
I have tried this code:
mentioned_users = []
for i in range(len(tweets_data)):
if tweets_data['mentionedUsers'][i]['username'] is not None:
mentioned_users.append(tweets_data['mentionedUsers'][i]['username'])
else:
mentioned_users.append(None)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-64-cc453018d33d> in <module>
3
4 for i in range(len(tweets_data)):
----> 5 if tweets_data['mentionedUsers'][i]['username'] is not None:
6 mentioned_users.append(tweets_data['mentionedUsers'][i]['username'])
7 else:
TypeError: list indices must be integers or slices, not str
Could anyone please tell me what's wrong with this? I believe the problem is in the [].If so how do I extract the data from list? Thank you for help!