简体   繁体   中英

Why am I getting np.NaN values when trying to add a column to a Pandas dataframe?

I have a pandas dataframe with date information stored as a string. I want to extract the month from each date directly, so I tried this:

import pandas as pd

df = pd.DataFrame([['2015-04-16', 5], ['2014-05-01', 6]],columns = ['date','units'])
df['month'] = df['month'].str[5,7]
print(df)

This gives the following output

    date  units  month
0  2015-04-16      5    NaN
1  2014-05-01      6    NaN

The dtype for the NaN's is float, and I have no idea why. Why doesn't this just create another column with the substrings?

If you're trying to slice each string to get the substring from 5 to 7, you need a : , not a , :

>>> df = pd.DataFrame([['2015-04-16', 5], ['2014-05-01', 6]],columns = ['date','units'])
>>> df['month'] = df['date'].str[5:7]
>>> print(df)
         date  units month
0  2015-04-16      5    04
1  2014-05-01      6    05

I think your problem is that your slicing is invalid:

In [7]:

df = pd.DataFrame([['2015-04-16', 5], ['2014-05-01', 6]],columns = ['date','units'])
df['date'].str[5,7]
​
Out[7]:
0   NaN
1   NaN
Name: date, dtype: float64

Compare with this:

t='2015-04-16'
t[5,7]

this raises a:

TypeError: string indices must be integers

I think you wanted:

In [18]:

df = pd.DataFrame([['2015-04-16', 5], ['2014-05-01', 6]],columns = ['date','units'])
df['month'] = df['date'].str[5:7]
df
Out[18]:
         date  units month
0  2015-04-16      5    04
1  2014-05-01      6    05

So as this is an invalid operation pandas is returning NaN

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