I have a data frame that I scraped using Tweepy looks like this:
index user entities.user.mentions
1 w [{'screen_name': 'a','name': '1'},{'screen_name': 'b','name': '2',}]
2 x [{'screen_name': 'c','name': '3'}]
3 y [{'screen_name': 'e','name': '5'},{'screen_name': 'f','name': '6',}]
4 z [{'screen_name': 'g','name': '7'}]
I want to create a column that has the value of the key 'screen_name'. In cases where there are multiple dictionaries in a row in 'entities.user.mentions', I would like to create another row with the same 'user' next the corresponding 'screenname'. This is what I'd like the new data frame to look like:
index user mention
1 w a
2 w b
3 x c
4 y e
5 y f
6 z g
I was able to do it with the following for loops, but it is taking much longer (5 minutes) than I want to run:
for x in range(len(df['entities.user.mentions'])):
d1 = {}
for y in range(len(df['emu'][x])):
d1[y] = df['entities.user.mentions'][x][y]['screen_name']
df['entities.user.mentions'][x] = d1
Then:
final_df = pd.DataFrame()
for x in range(len(df)):
for y in range(len(df['emu_sn'][x])):
a = df.iloc[x,]
a['entities.user.mentions'] = df['entities.user.mentions'][x][y]
final_df = final_df.append(a)
Would appreciate another solution that works faster than the above. Thank you in advance.