熊猫的拖放功能在UDF中不起作用

时间:2019-01-04 16:52:40

标签: python-3.x pandas

我环顾了其他问题,但找不到能解决我遇到的问题的方法。我正在清理ipython笔记本中的数据集。当我分别运行清理任务时,它们将按预期工作,但是当UDF中包含了replace()和drop()函数时,它们会遇到麻烦。具体来说,这些行在UDF中没有执行任何操作,但是会返回一个数据框,该数据框将按预期完成其他任务(即读取文件,设置索引并过滤选择日期)。

非常感谢您的帮助!

请注意,在此问题中,在UDF外部执行df.drop()和df.replace()命令都可以按预期工作。该功能在下面供您参考。问题出在最后两行“ station.replace()”和“ station.drop()”上。

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    

1 个答案:

答案 0 :(得分:0)

这里有一个错误:

station = station_data[station_data.index > '1984-09-29'] 

我正在使用旧表索引。我将其更正为:

station = station[station.index > '1984-09-29'] 

注意,我必须重新启动笔记本电脑,然后从顶部重新运行它才能正常工作。我相信这是UDF中的表名与已存储在内存中的表名冲突的问题。