Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
HeadOverFeet Avatar asked May 05 '18 09:05

HeadOverFeet


People also ask

How do you update a DataFrame in a for loop?

As Dataframe. iterrows() returns a copy of the dataframe contents in tuple, so updating it will have no effect on actual dataframe. So, to update the contents of dataframe we need to iterate over the rows of dataframe using iterrows() and then access each row using at() to update it's contents.


1 Answers

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 
like image 61
Mihai Alexandru-Ionut Avatar answered Oct 02 '22 10:10

Mihai Alexandru-Ionut