chained-assignment

Confusion re: pandas copy of slice of dataframe warning

Confusion re: pandas copy of slice of dataframe warning Question: I’ve looked through a bunch of questions and answers related to this issue, but I’m still finding that I’m getting this copy of slice warning in places where I don’t expect it. Also, it’s cropping up in code that was running fine for me previously, …

Total answers: 1

Correct way to set value on a slice in pandas

Correct way to set value on a slice in pandas Question: I have a pandas dataframe: data. it has columns [“name”, ‘A’, ‘B’] What I want to do (and works) is: d2 = data[data[‘name’] == ‘fred’] #This gives me multiple rows d2[‘A’] = 0 This will set the column A on the fred rows to …

Total answers: 1

Extracting specific selected columns to new DataFrame as a copy

Extracting specific selected columns to new DataFrame as a copy Question: I have a pandas DataFrame with 4 columns and I want to create a new DataFrame that only has three of the columns. This question is similar to: Extracting specific columns from a data frame but for pandas not R. The following code does …

Total answers: 10

Python: Pandas Dataframe how to multiply entire column with a scalar

Python: Pandas Dataframe how to multiply entire column with a scalar Question: How do I multiply each element of a given column of my dataframe with a scalar? (I have tried looking on SO, but cannot seem to find the right solution) Doing something like: df[‘quantity’] *= -1 # trying to multiply each row’s quantity …

Total answers: 12

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

why should I make a copy of a data frame in pandas Question: 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. For example, X = my_dataframe[features_list].copy() …instead of just X = my_dataframe[features_list] Why are they making a copy …

Total answers: 8

Checking whether data frame is copy or view in Pandas

Checking whether data frame is copy or view in Pandas Question: Is there an easy way to check whether two data frames are different copies or views of the same underlying data that doesn’t involve manipulations? I’m trying to get a grip on when each is generated, and given how idiosyncratic the rules seem to …

Total answers: 3

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

What rules does Pandas use to generate a view vs a copy? Question: 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 …

Total answers: 2

Pandas: Chained assignments

Pandas: Chained assignments Question: 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 code, where data is …

Total answers: 1

How to deal with SettingWithCopyWarning in Pandas

How to deal with SettingWithCopyWarning in Pandas Question: Background I just upgraded my Pandas from 0.11 to 0.13.0rc1. Now, the application is popping out many new warnings. One of them like this: E:FinReporterFM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value …

Total answers: 20

Pandas: Subindexing dataframes: Copies vs views

Pandas: Subindexing dataframes: Copies vs views Question: 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 …

Total answers: 1