1

When running my code I get the following message:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  df['detect'][df.index[0]] = df['event'][df.index[0]]

What is the correct way of setting the first value of a column equal to the first value of another column?

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64

1 Answers1

1

Use ix to perform the index label selection:

In [102]:

df = pd.DataFrame({'detect':np.random.randn(5), 'event':np.arange(5)})
df
Out[102]:
     detect  event
0 -0.815105      0
1 -0.656923      1
2 -1.417722      2
3  0.210070      3
4  0.211728      4
In [103]:

df.ix[0,'detect'] = df.ix[0,'event']
df
Out[103]:
     detect  event
0  0.000000      0
1 -0.656923      1
2 -1.417722      2
3  0.210070      3
4  0.211728      4

What you are doing is called chained indexing and may or may not work hence the warning

EdChum
  • 376,765
  • 198
  • 813
  • 562