I try to delete
some column and convert some value in column with
df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True)
df2['date'] = df2['date'].map(lambda x: str(x)[1:])
df2['date'] = df2['date'].str.replace(':', ' ', 1)
df2['date'] = pd.to_datetime(df2['date'])
and to all this string I get
df2.drop(df2.columns[[0, 1, 3]], axis=1, inplace=True)
C:/Users/����� �����������/Desktop/projects/youtube_log/filter.py:11: 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
What is problem there?
Your
df2
is a slice of another dataframe. You need to explicitly copy it withdf2 = df2.copy()
just prior to your attempt todrop
Consider the following dataframe:
Let me assign a slice of
df1
todf2
df2
is now a slice ofdf1
and should trigger those peskySettingWithCopyWarning
's if we try to change things indf2
. Let's take a look.No problems. How about:
There it is:
The problem is that pandas tries to be efficient and tracks that
df2
is pointing to the same data asdf1
. It is preserving that relationship. The warning is telling you that you shouldn't be trying to mess with the original dataframe via the slice.Notice that when we look at
df2
, row 'c' has been dropped.And looking at
df1
we see that row 'c' is still there.pandas made a copy of
df2
then dropped row 'c'. This is potentially inconsistent with what our intent may have been considering we madedf2
a slice of and pointing to same data asdf1
. So pandas is warning us.To not see the warning, make the copy yourself.