Home > Software design >  Pandas - adding new string column results in NaN
Pandas - adding new string column results in NaN

Time:10-29

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?

CodePudding user response:

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
  • Related