Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫不工作_Python_Pandas - Fatal编程技术网

Python 熊猫不工作

Python 熊猫不工作,python,pandas,Python,Pandas,我有一个包含NaN的数据帧: >>>df.head() Out[1]: JPM US SMALLER COMPANIES C ACC 1990-01-02 NaN 1990-01-03 NaN 1990-01-04 NaN 1990-01-05

我有一个包含NaN的数据帧:

>>>df.head()
Out[1]: 
            JPM US SMALLER COMPANIES C ACC
1990-01-02                             NaN
1990-01-03                             NaN
1990-01-04                             NaN
1990-01-05                             NaN
1990-01-08                             NaN
我有另一个数据帧,其中包含值:

>>>t.head()
Out[1]: 
1990-01-02    51.95
1990-01-03    52.63
1990-01-04    53.04
1990-01-05    52.07
1990-01-08    51.73
Name: JPM US SMALLER COMPANIES C ACC, dtype: float64
不幸的是,df.fillna似乎不适合我:

>>>df.fillna( t ).head()
Out[1]: 
            JPM US SMALLER COMPANIES C ACC
1990-01-02                             NaN
1990-01-03                             NaN
1990-01-04                             NaN
1990-01-05                             NaN
1990-01-08                             NaN

[5 rows x 1 columns]

为什么会这样?我使用的是pandas 0.13.1

您需要
就位

df[1].fillna(0, inplace=True)

您需要分配值df=df。fillna(t)

您有两个选项:

1) 每列的特定

cols_fillna = ['column1','column2','column3']
# replace 'NaN' with zero in these columns
 for col in cols_fillna:
     df[col].fillna(0,inplace=True)
     df[col].fillna(0,inplace=True)
2) 对于整个数据帧

df = df.fillna(0)
或者:

df=df.replace(np.nan,0)
#或您认为合适的任何其他值


当我在Na操作之后立即应用某些str.replace()操作时,df.replace(np.nan,0)或df.fillna(0)使我感到厌烦。。因此,请注意命令的顺序->first str.replace()而不是fillna()

Oops。我修好了。df应该是一个系列,而不是一个数据帧。在这之后,它就起作用了。在您使用数据帧的情况下,您可以使用DataFrame.where来使用另一帧的值来替换空值。@benjwadams我认为在这种情况下,使用或可能更好。它们是为此目的定制的,因此它们应该可以帮助您避免错误。您帮助我避免在我的pandas项目中创建这么多新对象。为什么这里需要
inplace=True
?这样
df
对象就可以在原地修改,而不是创建副本并修改此副本