简体   繁体   中英

Pandas - adding new string column results in NaN

I've had some trouble assigning a new column in a Pandas dataframe - I have got it working, but want to understand why it happens;

When I first tried to assign the ID to a string, the result was NaN..

df = pandas.json_normalize(data)
all_per = pandas.DataFrame()
for person in peopleList:
    all_per['id'] = person
    all_per['name'] = df['results.(id:'+person+').localizedFirstName'] + ' ' + \
                      df['results.(id:'+person+').localizedLastName']

Results:

    id          name
0  NaN    Adam Smith

However if I move the ID assignment down a bit, it works..

df = pandas.json_normalize(data)
all_per = pandas.DataFrame()
for person in peopleList:
    all_per['name'] = df['results.(id:'+person+').localizedFirstName'] + ' ' + \
                      df['results.(id:'+person+').localizedLastName']
    all_per['id'] = person

Results:

           name          id
0    Adam Smith    FQR4bL_80K

This took up a lot of my time, and I have no idea why it happened? Any ideas?

You can't add a scalar value. You have to enclose person into a list:

df = pandas.json_normalize(data)
all_per = pandas.DataFrame()
for person in peopleList:
    all_per['id'] = [person]  # <- HERE
    all_per['name'] = df['results.(id:'+person+').localizedFirstName'] + ' ' + \
                      df['results.(id:'+person+').localizedLastName']

Output:

>>> all_per

           id        name
0  FQR4bL_80K  Adam Smith

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM