I am trying to fillna a column of dataframe like the following,
df['temp'] = df['temp'].fillna(method='ffill')
and I am getting,
var/folders/qp/lp_5yt3s65q_pj__6v_kdvnh0000gn/T/ipykernel_10842/2929940072.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['temp'] = df['temp'].fillna(method='ffill')
I revised the code to the following but I am still getting the same error. Do you have any suggestions?
df.loc[:,'temp'] = df['temp'].fillna(method='ffill')
Related
I tried filling the NA values of a column in a dataframe with:
df1 = data.copy()
df1.columns = data.columns.str.lower()
df2 = df1[['passangerid', 'trip_cost','class']]
df2['class'] = df2['class'].fillna(0)
df2
Although getting this error:
:5: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation:
df2['class'] = df2['class'].fillna(0, axis = 0)
Can someone please help?
First of all I'd advise you to follow the warning message and read up on the caveats in the provided link.
You're getting this warning (not an error) because your df2 is a slice of your df1, not a separate DataFrame.
To avoid getting this warning you can use .copy() method as:
df2 = df1[['passangerid', 'trip_cost','class']].copy()
I have a pandas dataframe (pandas version '0.24.2') and a list which have the same length.
I want to add this list as a column to the dataframe.
I do this:
df.loc[:, 'new_column'] = pd.Series(my_List, index=df.index)
but I receive this warning:
.../anaconda/lib/python3.7/site-packages/pandas/core/indexing.py:362: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-
docs/stable/indexing.html#indexing-view-versus-copy
self.obj[key] = _infer_fill_value(value)
.../anaconda/lib/python3.7/site-packages/pandas/core/indexing.py:543: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
Am I doing something wrong?
This question already has answers here:
How to deal with SettingWithCopyWarning in Pandas
(20 answers)
Closed 2 years ago.
I tried the following code to convert a column to "date":
df.['DATE'] = pd.to_datetime(df['DATE'])
or
df.DATE = pd.to_datetime(df.DATE)
but I get the following error:
/Users/xyz/anaconda3/envs/sensor/lib/python3.6/site-packages/pandas/core/indexing.py:517:
SettingWithCopyWarning: A value is trying to be set on a copy of a
slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =
value instead
See the caveats in the documentation:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
I changed the code to the following:
df.loc[:,'DATE'] = pd.to_datetime(df.loc[:,'DATE'])
but I still get the same error.
same with this
for i in df.index:
df.loc[i,'DATE'] = pd.to_datetime(df.loc[i,'DATE'])
You need add copy:
df = data.loc[data.ID == 79]
to:
df = data.loc[data.ID == 79].copy()
If you modify values in df later you will find that the modifications do not propagate back to the original data (data), and that Pandas does warning.
The problem is in the code you have not shown us. Somewhere, you have done something like this:
df = other.loc[something]
That is the root cause of this error message. You need to assign using .loc or similar directly into the original DataFrame:
other.loc[something, 'DATE'] = whatever
trying to lower and strip a column in python 3 using panda, but getting the warning-- what is the right way so this warning will not come up
df["col1"] = df[["col1"]].apply(lambda x: x.str.strip())
df["col1"] = df[["col1"]].apply(lambda x: x.str.lower())
The warning
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self[k1] = value[k2]
how to remove the warning
To get rid of this warning apply it to a series instead of a dataframe. Using df[["col1"]] is creating a new dataframe that you are then setting to the column. If you instead just modify the column it'll be fine. Additionally, I chained the two together.
df["col1"] = df["col1"].str.strip().str.lower()
I'm just trying to convert a column of numeric strings to ints. This is what I'm trying:
df.date = df.date.astype(np.int64)
But I'm getting the warning:
/Users/austin/anaconda/lib/python3.5/site-packages/pandas/core/generic.py:2773:
SettingWithCopyWarning: A value is trying to be set on a copy of a
slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =
value instead
See the caveats in the documentation:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self[name] = value
Not sure what this means. I also tried:
df.date = df.date.apply(int)
And I get the same warning as above.
Why doesn't this work and what's the proper way?
astype function returns a new array. You need to assign the result:
date = date.astype(int)
x = pd.DataFrame(['20.1','19.1','12.3'])
x[0].convert_objects(convert_numeric=True)