python - pandas.dataframe.fillna: NotImplementedError
问题描述
我想使用 pandas 中的 fillna 命令来估算数据框。这是我的代码片段:
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 文件)转换为更合适的格式。然后我想沿列轴使用 fillna 命令填充缺失的数据。
该代码的结构是为了读取多个 .csv 文件,因此具有不必要的命令,例如“df=[]”和“for 循环”,但为了简化起见,我首先尝试了这段代码,以确保我得到了这个错误. 随时询问有关此错误的更多信息。谢谢!
解决方案
推荐阅读
- ruby - 一串两种颜色
- php - 如何将大部分一致的文件名解析为有意义的部分?
- python - 如何在选项卡内的 tkinter 框架中的多次输入(有条件)期间使滚动条工作?
- html - 不能把它放在内联块中
- javascript - 如何将外部 HTML 页面添加到我的当前页面,这是另一个 HTML
- javascript - 按数字索引对javascript中的关联数组进行排序
- http - Google Cloudfunction 中的最大并发 http 连接数
- c - 使用 stm32f042 作为 spi 从机
- python - 具有不相等行和列的两个二维 numpy 数组的交集
- html - 如何仅使用 CSS 在 HTML 按钮上添加 FontAwsome 图标?