I’m currently scraping some user//follower information from the Twitter API using Tweepy. I’m currently storing the data as a a dictionary where every key is a unique twitter user and the values are a list of ID’s for their followers.
The data looks like this:
{'realDonaldTrump': [
123456,
123457,
123458,
...
],
'BarackObama' : [
999990,
999991,
999992,
...
]}
What I need is a dataframe that looks like this:
user follower
realDonaldTrump 123456
realDonaldTrump 123457
realDonaldTrump 123458
... ...
BarackObama 999990
BarackObama 999991
BarackObama 999992
... ...
I’ve already tried:
df = pd.DataFrame.from_dict(followers)
but it gives me a new column for each key, and doesn’t handle uneven length of follower lists.
Is there a smart way to convert the dictionary structure I have into a dataframe? Or should I store the initial data differently?
3
Answers
Use list comprehension for tuples and pass to DataFrame constructor:
Create a compatible dict:
Output:
Result: