Python 3.x Pandas Drop和Replace函数在UDF中不起作用
我环顾四周看了看其他问题,但没有找到解决问题的方法。我正在清理ipython笔记本中的数据集。当我单独运行清理任务时,它们会按预期工作,但当它们包含在UDF中时,替换和删除功能会出现问题。具体地说,这些行在UDF中没有做任何事情,但是,返回一个数据帧,按照预期完成其他任务,即读取文件、设置索引和筛选选择日期 非常感谢您的帮助 注意,在这个问题中,df.drop和df.replace命令在UDF之外执行时都能正常工作。下面的函数供您参考。问题在于最后两条线路station.replace和station.dropPython 3.x Pandas Drop和Replace函数在UDF中不起作用,python-3.x,pandas,Python 3.x,Pandas,我环顾四周看了看其他问题,但没有找到解决问题的方法。我正在清理ipython笔记本中的数据集。当我单独运行清理任务时,它们会按预期工作,但当它们包含在UDF中时,替换和删除功能会出现问题。具体地说,这些行在UDF中没有做任何事情,但是,返回一个数据帧,按照预期完成其他任务,即读取文件、设置索引和筛选选择日期 非常感谢您的帮助 注意,在这个问题中,df.drop和df.replace命令在UDF之外执行时都能正常工作。下面的函数供您参考。问题在于最后两条线路station.replace和stat
def read_file(file_path):
'''Function to read in daily x data'''
if os.path.exists(os.getcwd()+'/'+file_path) == True:
station = pd.read_csv(file_path)
else:
!unzip alldata.zip
station = pd.read_csv(file_path)
station.set_index('date',inplace=True) #put date in the index
station = station_data[station_data.index > '1984-09-29'] #removes days where there is no y-data
station.replace('---','0',inplace=True)
station.drop(columns=['Unnamed: 0'],axis=1,inplace=True) #drop non-station columns
这里有一个错误:
station = station_data[station_data.index > '1984-09-29']
我使用的是一个旧的表索引。我更正为:
station = station[station.index > '1984-09-29']
请注意,我必须重新启动笔记本电脑,并从顶部重新运行它才能工作。我认为UDF中的表名与存储在内存中的表名冲突是一个问题