python - pandas standalone series and from dataframe different behavior -
here code , warning message. if change s
standalone series
using s = pd.series(np.random.randn(5))
, there no such errors. using python 2.7 on windows.
it seems series created standalone , series created column of data frame different behavior? thanks.
my purpose change series value itself, other change on copy.
source code,
import pandas pd sample = pd.read_csv('123.csv', header=none, skiprows=1, dtype={0:str, 1:str, 2:str, 3:float}) sample.columns = pd.index(data=['c_a', 'c_b', 'c_c', 'c_d']) sample['c_d'] = sample['c_d'].astype('int64') s = sample['c_d'] #s = pd.series(np.random.randn(5)) in range(len(s)): if s.iloc[i] > 0: s.iloc[i] = s.iloc[i] + 1 else: s.iloc[i] = s.iloc[i] - 1
warning message,
c:\python27\lib\site-packages\pandas\core\indexing.py:132: settingwithcopywarning: value trying set on copy of slice dataframe see caveats in documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._setitem_with_indexer(indexer, value)
content of 123.csv,
c_a,c_b,c_c,c_d hello,python,numpy,0.0 hi,python,pandas,1.0 ho,c++,vector,0.0 ho,c++,std,1.0 go,c++,std,0.0
edit 1, seems lambda solution not work, tried print s
before , after, same value,
import pandas pd sample = pd.read_csv('123.csv', header=none, skiprows=1, dtype={0:str, 1:str, 2:str, 3:float}) sample.columns = pd.index(data=['c_a', 'c_b', 'c_c', 'c_d']) sample['c_d'] = sample['c_d'].astype('int64') s = sample['c_d'] print s s.apply(lambda x:x+1 if x>0 else x-1) print s 0 0 1 1 2 0 3 1 4 0 name: c_d, dtype: int64 backend tkagg interactive backend. turning interactive mode on. 0 0 1 1 2 0 3 1 4 0
regards, lin
by doing s = sample['c_d']
, if make change value of s
original dataframe sample
changes. that's why got warning.
you can s = sample[c_d].copy()
instead, changing value of s
doesn't change value of c_d
column of dataframe sample
.
Comments
Post a Comment