chained-assignment

Pandas SettingWithCopyWarning [duplicate]

♀尐吖头ヾ 提交于 2019-11-26 22:14:38
This question already has an answer here: How to deal with SettingWithCopyWarning in Pandas? 13 answers Python 3.4 and Pandas 0.15.0 df is a dataframe and col1 is a column. With the code below, I'm checking for the presence of the value 10 and replacing such values with 1000. df.col1[df.col1 == 10] = 1000 Here's another example. This time, I'm changing values in col2 based on index. df.col2[df.index == 151] = 500 Both these produce the warning below: -c:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the the caveats in the documentation: http:/

Pandas: SettingWithCopyWarning [duplicate]

一个人想着一个人 提交于 2019-11-26 15:55:19
问题 This question already has answers here : How to deal with SettingWithCopyWarning in Pandas? (14 answers) Closed 11 months ago . I'd like to replace values in a Pandas DataFrame larger than an arbitrary number (100 in this case) with NaN (as values this large are indicative of a failed experiment). Previously I've used this to replace unwanted values: sve2_all[sve2_all[' Hgtot ng/l'] > 100] = np.nan However, I got the following error: -c:3: SettingWithCopyWarning: A value is trying to be set

why should I make a copy of a data frame in pandas

江枫思渺然 提交于 2019-11-26 12:10:45
When selecting a sub dataframe from a parent dataframe, I noticed that some programmers make a copy of the data frame using the .copy() method. Why are they making a copy of the data frame? What will happen if I don't make a copy? This expands on Paul's answer. In Pandas, indexing a DataFrame returns a reference to the initial DataFrame. Thus, changing the subset will change the initial DataFrame. Thus, you'd want to use the copy if you want to make sure the initial DataFrame shouldn't change. Consider the following code: df = DataFrame({'x': [1,2]}) df_sub = df[0:1] df_sub.x = -1 print(df)

Pandas still getting SettingWithCopyWarning even after using .loc

匆匆过客 提交于 2019-11-26 08:25:58
问题 At first, I tried writing some code that looked like this: import numpy as np import pandas as pd np.random.seed(2016) train = pd.DataFrame(np.random.choice([np.nan, 1, 2], size=(10, 3)), columns=[\'Age\', \'SibSp\', \'Parch\']) complete = train.dropna() complete[\'AgeGt15\'] = complete[\'Age\'] > 15 After getting SettingWithCopyWarning, I tried using.loc: complete.loc[:, \'AgeGt15\'] = complete[\'Age\'] > 15 complete.loc[:, \'WithFamily\'] = complete[\'SibSp\'] + complete[\'Parch\'] > 0

Pandas SettingWithCopyWarning [duplicate]

一曲冷凌霜 提交于 2019-11-26 08:16:34
问题 This question already has an answer here: How to deal with SettingWithCopyWarning in Pandas? 14 answers Python 3.4 and Pandas 0.15.0 df is a dataframe and col1 is a column. With the code below, I\'m checking for the presence of the value 10 and replacing such values with 1000. df.col1[df.col1 == 10] = 1000 Here\'s another example. This time, I\'m changing values in col2 based on index. df.col2[df.index == 151] = 500 Both these produce the warning below: -c:1: SettingWithCopyWarning: A value

Pandas: Subindexing dataframes: Copies vs views

天大地大妈咪最大 提交于 2019-11-26 06:38:15
问题 Say I have a dataframe import pandas as pd import numpy as np foo = pd.DataFrame(np.random.random((10,5))) and I create another dataframe from a subset of my data: bar = foo.iloc[3:5,1:4] does bar hold a copy of those elements from foo ? Is there any way to create a view of that data instead? If so, what would happen if I try to modify data in this view? Does Pandas provide any sort of copy-on-write mechanism? 回答1: Your answer lies in the pandas docs: returning-a-view-versus-a-copy. Whenever

why should I make a copy of a data frame in pandas

╄→гoц情女王★ 提交于 2019-11-26 03:27:52
问题 When selecting a sub dataframe from a parent dataframe, I noticed that some programmers make a copy of the data frame using the .copy() method. Why are they making a copy of the data frame? What will happen if I don\'t make a copy? 回答1: This expands on Paul's answer. In Pandas, indexing a DataFrame returns a reference to the initial DataFrame. Thus, changing the subset will change the initial DataFrame. Thus, you'd want to use the copy if you want to make sure the initial DataFrame shouldn't

What rules does Pandas use to generate a view vs a copy?

浪尽此生 提交于 2019-11-26 03:03:15
I'm confused about the rules Pandas uses when deciding that a selection from a dataframe is a copy of the original dataframe, or a view on the original. If I have, for example, df = pd.DataFrame(np.random.randn(8,8), columns=list('ABCDEFGH'), index=range(1,9)) I understand that a query returns a copy so that something like foo = df.query('2 < index <= 5') foo.loc[:,'E'] = 40 will have no effect on the original dataframe, df . I also understand that scalar or named slices return a view, so that assignments to these, such as df.iloc[3] = 70 or df.ix[1,'B':'E'] = 222 will change df . But I'm lost

What rules does Pandas use to generate a view vs a copy?

自作多情 提交于 2019-11-26 01:52:13
问题 I\'m confused about the rules Pandas uses when deciding that a selection from a dataframe is a copy of the original dataframe, or a view on the original. If I have, for example, df = pd.DataFrame(np.random.randn(8,8), columns=list(\'ABCDEFGH\'), index=range(1,9)) I understand that a query returns a copy so that something like foo = df.query(\'2 < index <= 5\') foo.loc[:,\'E\'] = 40 will have no effect on the original dataframe, df . I also understand that scalar or named slices return a view,

Pandas: Chained assignments [duplicate]

大憨熊 提交于 2019-11-26 00:29:03
问题 This question already has an answer here: How to deal with SettingWithCopyWarning in Pandas? 14 answers I have been reading this link on \"Returning a view versus a copy\". I do not really get how the chained assignment concept in Pandas works and how the usage of .ix() , .iloc() , or .loc() affects it. I get the SettingWithCopyWarning warnings for the following lines of codes, where data is a Panda dataframe and amount is a column (Series) name in that dataframe: data[\'amount\'] = data[\