我想在熊猫中使用pandas命令来计算数据。下面是我的代码片段:
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错误:
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循环’,但为了简化起见,我首先尝试了这段代码,以确保并得到了这个错误。请随时询问有关此错误的更多信息。谢谢!
发布于 2018-06-28 12:12:51
发布于 2020-08-23 19:55:42
当数据帧在同一行中同时包含in和NaNs时,我在使用ffill (fillna(method='ffill')的同义词)填充跨列的缺失值时遇到了此错误。
解决办法是使用inplace=False
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)https://stackoverflow.com/questions/51064157
复制相似问题