1

I have a Pandas dataframe like this (it's a list of Twitter handles):

          label  favees
0       1NewsNZ       1
1          mhjb       1
2  citizenai_nz       1
...

I want to flesh it out with some info from the related Twitter profiles. The python-twitter UsersLookup function returns a list of dicts like this:

{"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, ...}

What I want to do is copy the values in those three columns in profiles for all 100 rows into the chunk dataframe, all at once. The last three lines below won't work, but perhaps give the idea of what I'm trying to do:

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']

After that I'd append the fleshed out rows to a new CSV. (df_chunks comes from https://stackoverflow.com/a/44729807/1876628)

I have a feeling the answer has something to do with dataframe.map.

mhjb
  • 141
  • 1
  • 5

1 Answers1

0

Not sure it's the best way, but this seems to work:

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]

Thanks to this answer for the clue I needed.

Do sing out if there is a more elegant way…

mhjb
  • 141
  • 1
  • 5