首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pandas.dataframe.fillna: NotImplementedError

pandas.dataframe.fillna: NotImplementedError
EN

Stack Overflow用户
提问于 2018-06-27 13:32:43
回答 2查看 1.8K关注 0票数 2

我想在熊猫中使用pandas命令来计算数据。下面是我的代码片段:

代码语言:javascript
复制
import glob
import pandas as pd
files=glob.glob("IN.201*.csv")
i=0
n=1    
#the while loops are for reading and writing different subsets of the table into 
#different .txt files:
while i<15:
   j=0
   while j<7:
    dfs=[]
    m=1
   #for loop over only one file for testing:
       for file in files[:1]:
        z=i+1
        #reading subset of the dataframe:
        k=float(68.109375)+float(1.953125)*i
        k1=float(68.109375)+float(1.953125)*z
        l=float(8.0)+float(4)*j
        l1=float(8.0)+float(4)*(j+1)
        df=pd.read_csv(path+file).query( '@k <= lon < @k1 and @l < lat <= @l1')[['lon','lat','country','avg']]
        #renaming columns in df: 
        df.rename(columns={"avg":"Day"+str(m)}, inplace=True)
        #print(df)
        m=m+1
        dfs.append(df)
  #imputation: 
    df_final=dfs[0].fillna(method='bfill', axis='columns', inplace=True).fillna(method='ffill', axis=1, inplace=True)
  #writing to a txt file:
    with open('Region_'+str(n), 'w+') as f:
        df_final.to_csv(f)

    n=n+1
    j=j+1
   i=i+1

错误:

代码语言:javascript
复制
 Traceback (most recent call last):
 File "imputation_test.py", line 42, in <module>
 df_final=dfs[0].fillna(method='bfill', axis='columns', inplace=True).fillna(
 method='ffill', axis=1, inplace=True)
 File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site- 
 packages\p
 andas\core\frame.py", line 3787, in fillna
 downcast=downcast, **kwargs)
 File "C:\Users\DELL\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 5359, in fillna
raise NotImplementedError()
 NotImplementedError

代码的动机:

实际上,我希望将一个.csv文件读入由这个表的不同子集组成的多个数据文件中(因此,我使用了所有循环),以便将.csv文件/s(实际上我想要对多个.csv文件这样做)重新排列并拆分成一种更合适的格式。然后,我希望在列轴上使用along命令来填充丢失的数据。

代码是为读取多个.csv文件而构造的,因此有不必要的命令,如'df=‘和'for循环’,但为了简化起见,我首先尝试了这段代码,以确保并得到了这个错误。请随时询问有关此错误的更多信息。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-28 12:12:51

使用bfillffillaxis=1

代码语言:javascript
复制
dfs = dfs.bfill(axis=1).ffill(axis=1)

部分问题是inplace=True和链接方法。inplace=True返回一个空对象,因此没有什么可以调用链式方法。第二部分是fillna(method='ffill')可以缩短为ffill()

票数 5
EN

Stack Overflow用户

发布于 2020-08-23 19:55:42

当数据帧在同一行中同时包含in和NaNs时,我在使用ffill (fillna(method='ffill')的同义词)填充跨列的缺失值时遇到了此错误。

解决办法是使用inplace=False

代码语言:javascript
复制
df = pd.DataFrame(data={'a': [1]})
df = df.reindex(columns=['a', 'b', 'c'])  # columns b and c contain NaN
df.ffill(axis='columns', inplace=True)  # raises NotImplementedError
df = df.ffill(axis='columns')  # works (inplace defaults to False)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51064157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档