【发布时间】:2019-05-14 13:13:07
【问题描述】:
我想将 index = [1,3,5,7,9] 处的数据框的值更改为其他值。但是下面的代码根本不能工作。
df = pd.DataFrame({'col1': [1000]*12})
s1 = pd.Series([i for i in range(2,7)])#supposedly new values
index = [2*i+1 for i in range(5)]#Given indices
df.iloc[index]['col1'] = s1#attempt to modify the values
print(df)
输出如下:
col1
0 1000
1 1000
2 1000
3 1000
4 1000
5 1000
6 1000
7 1000
8 1000
9 1000
10 1000
11 1000
C:/Users/User/Desktop/all python file/3.2.4/iloc_assign.py:13: 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 pandas list indexing series