简体   繁体   English

Python:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值

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

My pandas dataframe: 我的熊猫数据帧:

dframe = pd.DataFrame({"A":list("abcde"), "B":list("aabbc"), "C":[1,2,3,4,5]},  index=[10,11,12,13,14])

    A   B   C
10  a   a   1
11  b   a   2
12  c   b   3
13  d   b   4
14  e   c   5

My desired output: 我想要的输出:

    A   B   C   a   b   c
10  a   a   1   1   None    None
11  b   a   2   2   None    None
12  c   b   3   None    3   None
13  d   b   4   None    4   None
14  e   c   5   None    None    5

Idea is to create new column based on values in 'B' column, copy respective values in 'C' column and paste them in newly created columns. 想法是根据“B”列中的值创建新列,复制“C”列中的相应值并将其粘贴到新创建的列中。 Here is my code: 这是我的代码:

lis = sorted(list(dframe.B.unique()))

#creating empty columns
for items in lis:
   dframe[items] = None


 #here copy and pasting
    for items in range(0, len(dframe)):
        slot = dframe.B.iloc[items]
        dframe[slot][items] = dframe.C.iloc[items]

I ended up with this error: 我最终得到了这个错误:

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
  app.launch_new_instance()

This code worked well in Python 2.7 but not in 3.x. 此代码在Python 2.7中运行良好,但在3.x中运行不佳。 Where I'm going wrong? 我哪里错了?

Start with 从...开始

to_be_appended = pd.get_dummies(dframe.B).replace(0, np.nan).mul(dframe.C, axis=0)

Then concat 然后结束

dframe = pd.concat([dframe, to_be_appended], axis=1)

Looks like: 好像:

print dframe

    A  B  C    a    b    c
10  a  a  1  1.0  NaN  NaN
11  b  a  2  2.0  NaN  NaN
12  c  b  3  NaN  3.0  NaN
13  d  b  4  NaN  4.0  NaN
14  e  c  5  NaN  NaN  5.0

Notes for searching. 搜索注意事项。

This is combining one hot encoding with a broadcast multiplication. 这是将一个热编码与广播乘法相结合。

Chained assignment will now by default warn if the user is assigning to a copy. 现在,如果用户正在分配副本,则链接分配将默认发出警告。

This can be changed with the option mode.chained_assignment, allowed options are raise/warn/None. 这可以通过选项mode.chained_assignment更改,允许的选项是raise / warn / None。 See the docs. 查看文档。

In [5]: dfc = DataFrame({'A':['aaa','bbb','ccc'],'B':[1,2,3]}) 在[5]中:dfc = DataFrame({'A':['aaa','bbb','ccc'],'B':[1,2,3]})

In [6]: pd.set_option('chained_assignment','warn') 在[6]中:pd.set_option('chained_assignment','warn')

The following warning / exception will show if this is attempted. 如果尝试这样,将显示以下警告/异常。

In [7]: dfc.loc[0]['A'] = 1111 在[7]中:dfc.loc [0] ['A'] = 1111

Traceback (most recent call last) ... SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. 回溯(最近一次调用最后一次)... SettingWithCopyWarning:尝试在DataFrame的切片副本上设置一个值。 Try using .loc[row_index,col_indexer] = value instead Here is the correct method of assignment. 尝试使用.loc [row_index,col_indexer] = value这是正确的赋值方法。

In [8]: dfc.loc[0,'A'] = 11 在[8]中:dfc.loc [0,'A'] = 11

In [9]: dfc 在[9]中:dfc

 A  B

0 11 1 0 11 1

1 bbb 2 1 bbb 2

2 ccc 3 2 ccc 3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas DataFrame:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 - Pandas DataFrame: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 对于循环错误,SettingWithCopyWarning:正在尝试在 DataFrame 中的切片副本上设置值 - For loop error, SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame SettingWithCopyWarning:试图在来自 DataFrame 的切片副本上设置一个值警告 - SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame WARNING SettingWithCopyWarning:试图在DataFrame的切片副本上设置一个值 - SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 更改熊猫列中的值:SettingWithCopyWarning:正在尝试在数据帧错误中的切片副本上设置值 - Changing values in pandas columns: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame error 循环导致错误:“SettingWithCopyWarning:试图在 DataFrame 的切片副本上设置值” - Loop led to Error: 'SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame' SettingWithCopyWarning:试图在 DataFrame 中切片的副本上设置值。 尝试使用 .loc[row_indexer,col_indexer] = value 代替, - 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, Python Pandas 警告:试图在 DataFrame 的切片副本上设置值 - Python Pandas Warning: A value is trying to be set on a copy of a slice from a DataFrame python pandas:尝试在DataFrame的切片副本上设置一个值 - python pandas: A value is trying to be set on a copy of a slice from a DataFrame Python 的问题是试图在 DataFrame 的切片副本上设置值 - Python problem with A value is trying to be set on a copy of a slice from a DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM