python settingwithcopywarning_数据框警告:Python中的SettingWithCopyWarning

Processing file from

http://portal.amfiindia.com/spages/NAV0.txt

to get output as follows:

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,

..

..

..

I have tried using below code but getting below warning.

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: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\ipykernel__main__.py:13:

SettingWithCopyWarning: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\ipykernel__main__.py:17:

SettingWithCopyWarning: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\ipykernel__main__.py:20:

SettingWithCopyWarning: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\ipykernel__main__.py:21:

SettingWithCopyWarning: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\ipykernel__main__.py:22:

SettingWithCopyWarning: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

c:\users\administrator\appdata\local\programs\python\python35-32\lib\site-packages\ipykernel__main__.py:23:

SettingWithCopyWarning: A value is trying to be set on a copy of a

slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =

value instead

Csv file is getting generated as expected but how can I overcome this warning?

I have tried using

fil_df.loc[ pandas.to_numeric(fil_df['Net Asset Value'],errors='coerce').map(lambda x: '%2.7f' % x]

but it didnt help.

Help would be appreciated.

解决方案

I think you need add copy:

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

If you modify values in fil_df later you will find that the modifications do not propagate back to the original data (df), and that Pandas does warning.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值