我使用pandas从云端读取我的csv文件,我使用replace()并希望0成为缺失值,但它似乎不起作用。
我使用 Google 的 colab
我尝试了两种方法:
user_data = user_data.replace(0,np.nan) # first
user_data.replace(0,np.nan,inplace = True) # second
user_data.head() # I use this to view the data.
但是数据和我第一次读的时候一样,0没有变化
这是我读取文件的函数,我使用的是block方法
# Read function
def get_df2(file):
mydata2 = []
for chunk in pd.read_csv(file,chunksize=500000,header = None,sep='\t'):
mydata2.append(chunk)
user_data = pd.concat(mydata2,axis=0)
names2=['user_id','age','gender','area','status']
user_data.columns = names2
return user_data
# read
user_data_path = 'a_url'
user_data = get_df2(user_data_path)
user_data.head()
注意:我的代码没有报告错误,它输出结果,但这不是我想要的
最佳答案
您的 0
可能只是字符串,请尝试使用:
user_data = user_data.replace('0', np.nan)
关于python - 我无法使用 python 的 "replace"使我的 0 成为缺失值(0->np.nan),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902684/