HeadOverFeet
HeadOverFeet

Reputation: 788

Changing value in data frame column in a loop python

I am new to Python pandas library and using data frames. I am using Jupyter. I kind of lost with this syntax.

I want to loop through rows and set new value to column new_value. I thought I would do it like this, but it raises an error.

df_merged['new_value'] = 0

for i, row in df_merged.iterrows():
    df_merged['new_value'][i] = i

I also tried to do a calculation like:

 df_merged['new_value'][i] = df_merged['move_%'] * df_merged['value']

But it doesnt work.

I am getting this error:

/usr/lib/python3.4/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas- docs/stable/indexing.html#indexing-view-versus-copy after removing the cwd from sys.path.

What I am doing wrong here?
Thanks.

Upvotes: 5

Views: 16132

Answers (5)

tartaruga_casco_mole
tartaruga_casco_mole

Reputation: 1193

For a loop update in pandas dataframe:

for i, row in df_merged.iterrows():
     df_merged.set_value(i,'new_value',i)

Should be able to update values in pandas dataframe.

FutureWarning: set_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead.

for i, row in df_merged.iterrows():
     df_merged.at[i,'new_value'] = i

Should be preferred.

Upvotes: 2

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48337

You can use just this:

df_merged['new_value'] = df.index

You can also use apply method.

df_merged['new_value'] = df_merged.apply(lambda row : row.name, axis=1)

I am getting this error : A value is trying to be set on a copy of a slice from a DataFrame

It's not a error, it's just a warning message.

From this answer:

The SettingWithCopyWarning was created to flag potentially confusing "chained" assignments, such as the following, which don't always work as expected, particularly when the first selection returns a copy.

You can avoid this warning message using pd.DataFrame.loc method.

for i, row in df_merged.iterrows():
    df_merged.loc[i,'price_new']  = i 

Upvotes: 3

Deepak
Deepak

Reputation: 149

This is not an error. It simply saying that the data frame _merged is initialised as a view of a parent daraframe and thus isn’t a data frame by itself, therefore cannot take values. That’s probably why when you check the value of the merged data frame after this step it remains the same as the original. You have two options: make your _merged data frame itself a copy by using the .copy() method when you initialise it from its parent data frame. Or in the loop or the computation set the values to the parent data frame using the same calculations or indexes done on merged data frame. I’d recommend the first method because I don’t think memory is a constraint for you and you want the values changed in the new data frame. Plus it is as straightforward as can be.

Upvotes: 1

HeadOverFeet
HeadOverFeet

Reputation: 788

This works also fine:

df_merged['price_new'] = 0

for i, row in df_merged.iterrows():
     df_merged.loc[i,'price_new']  = i 

Upvotes: 1

Mathew Savage
Mathew Savage

Reputation: 168

If you want to perform a multiplication on two columns, you don't have to do it row-wise, the following should work:

df_merged['new_value'] = df_merged['move_%'] * df_merged['value']

Upvotes: 0

Related Questions