首页 文章

Dataframe警告:在python中设置WithCopyWarning

提问于
浏览
2

处理文件来自
http://portal.amfiindia.com/spages/NAV0.txt
得到如下输出:
31012017,1,1,135765,12,10.8536000,
31012017,1,1,135762,12,10.8543000,
31012017,1,1,135760,12,10.6599000,
31012017,1,1,135759,12,10.6554000,
31012017,1,1,135763,12,10.8536000,
..
..
..

我尝试使用下面的代码,但得到低于警告 .

CODE:

import pandas
import numpy as np

#Sample file for NAV0.txt can be downloaded from url: http://portal.amfiindia.com/spages/NAV0.txt
#creating pandas with selected columns
df=pandas.read_table('NAV0.txt',sep=';',usecols=['Date','Scheme Code','Net Asset Value'])

#converting column with name 'Scheme Code' to digit to remove string part
fil_df=df[df['Scheme Code'].apply(lambda x : str(x).isdigit())]

#converting column with name 'Net Asset value' to numberic and set each value with 7 decimal places 
fil_df['Net Asset Value']=pandas.to_numeric(fil_df['Net Asset Value'],errors='coerce')
fil_df['Net Asset Value']=fil_df['Net Asset Value'].map(lambda x: '%2.7f' % x)


#Formating Date column as YYYMMDD
fil_df['Date']=pandas.to_datetime(fil_df['Date']).dt.strftime('%d%m%Y')

#adding extra column in dataframe
fil_df['ser1']=1
fil_df['ser2']=1
fil_df['period']=12
fil_df['lcol']=''
fil_df=fil_df[['Date','ser1','ser2','Scheme Code','period','Net Asset Value','lcol']]

#Converting datafile to csv
fil_df.to_csv('NAV_1.csv',index=False,header=None)
fil_df.dtypes

ERROR:

c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:12:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 . 尝试使用.loc [row_indexer,col_indexer] = value而不是c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:13:SettingWithCopyWarning:值正在尝试从DataFrame设置切片的副本 . 尝试使用.loc [row_indexer,col_indexer] = value而不是c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:17:SettingWithCopyWarning:值正在尝试从DataFrame设置切片的副本 . 尝试使用.loc [row_indexer,col_indexer] = value而不是c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:20:SettingWithCopyWarning:值正在尝试从DataFrame设置切片的副本 . 尝试使用.loc [row_indexer,col_indexer] = value而不是c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:21:SettingWithCopyWarning:值正在尝试从DataFrame设置切片的副本 . 尝试使用.loc [row_indexer,col_indexer] = value而不是c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:22:SettingWithCopyWarning:值正在尝试从DataFrame设置切片的副本 . 尝试使用.loc [row_indexer,col_indexer] = value而不是c:\ users \ administrator \ appdata \ local \ programs \ python \ python35-32 \ lib \ site-packages \ ipykernel__main __ . py:23:SettingWithCopyWarning:值正在尝试从DataFrame设置切片的副本 . 尝试使用.loc [row_indexer,col_indexer] = value

Csv文件按预期生成,但我如何克服此警告?我试过用
fil_df.loc[ pandas.to_numeric(fil_df['Net Asset Value'],errors='coerce').map(lambda x: '%2.7f' % x]
但它没有帮助 .
帮助将不胜感激 .

3 回答

  • 0

    我想你需要添加copy

    fil_df=df[df['Scheme Code'].apply(lambda x : str(x).isdigit())].copy()
    

    如果稍后修改 fil_df 中的值,您会发现修改不会传播回原始数据( df ),并且Pandas会发出警告 .

  • 0

    如果你知道你的代码在做什么,你可以使用

    pd.options.mode.chained_assignment = None  # default='warn'
    

    在您的代码中禁用此警告 .

  • 1

    你'll get to the heart of the matter in adding new columns to a DataFrame from this guy' s 2017编辑到这个answer . 基本上路线是使用 .assign('newCol' = enumerableValues )

相关问题