Reputation: 1349
How can I avoid the warning SettingWithCopyWarning
in this case?
Normally, it is enough to use a copy()
of a previously created DataFrame. This does not make sense in this situation:
import pandas as pd
import numpy as np
df = pd.DataFrame({"a": [7, 2, 3], "b": [4, 5, 6], "c": [np.nan, np.nan, np.NaN]})
df.c.iloc[0] = 100
.
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
self._setitem_with_indexer(indexer, value)
Upvotes: 2
Views: 2337
Reputation: 862841
Solution if default RangeIndex
is use DataFrame.loc
with specify index and column value:
df.loc[0, 'c'] = 100
print (df)
a b c
0 7 4 100.0
1 2 5 NaN
2 3 6 NaN
If want set by positions with DataFrame.iloc
is necessry Index.get_loc
for position of column c
:
df.iloc[0, df.columns.get_loc('c')] = 100
Upvotes: 3