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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With