【发布时间】:2016-11-03 03:09:45
【问题描述】:
如果我使用 .loc 创建 another_df 数据框,我不明白为什么会发生这种情况
>>> df = DataFrame({'a':range(0,10), 'b':range(10,20), 'c':range(20,30)}, index = range(0,10), columns=['a', 'b', 'c'])
>>> df
a b c
0 0 10 20
1 1 11 21
2 2 12 22
3 3 13 23
4 4 14 24
5 5 15 25
6 6 16 26
7 7 17 27
8 8 18 28
9 9 19 29
>>> another_df = df.loc[(df.a>4)&(df.b>14)&(df.c>24),:]
>>> another_df
a b c
5 5 15 25
6 6 16 26
7 7 17 27
8 8 18 28
9 9 19 29
>>> another_df['d'] = 'a random string'
<string>: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: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
【问题讨论】:
标签: python python-2.7 pandas indexing dataframe