提问人:NiKiuS 提问时间:11/9/2023 最后编辑:NiKiuS 更新时间:11/16/2023 访问量:43
如果其他列具有给定值,则在尝试将列的值复制到另一个列时,如何解决 Pandas SettingWithCopyWarning 错误?
How can I solve Pandas SettingWithCopyWarning error while trying to copy a value of a column to another if other have a given value?
问:
我有一个大数据帧,我需要使用适用于 3 个不同列的公式创建一个新列,如果第 1 列具有给定值,则将第 2 列的值分配给第 3 列。问题是我所做的一切都收到了很多错误,比如:
SettingWithCopyWarning:尝试在 DataFrame 中的切片副本上设置值。尝试改用 .loc[row_indexer,col_indexer] = value
FutureWarning:设置不兼容的 dtype 项已弃用,并且将来会引发 pandas 错误。
所以,我需要你的帮助才能解决它,因为我找不到一个积极的方法来解决这个问题。
数据帧形状约为 49MM 行 x 100 列,在本例中,我仅使用 2 列作为示例,但我需要执行许多新列,这些新列同时在公式中组合了数据帧的几列。
这些都是我的尝试,但它们都给了我熊猫错误。
df2 = df[['Unit','Qty']]
# 1st try: Copy Qty column to QtyKG and replace with zeros where Unit
# location isn't 'KG'.
df2['QtyKG'] = df['Qty'].copy()
df2.loc[df2['Unit'] != 'KG','QtyKG'] = 0
# 2nd try: Create a new column named QtyKG filled with zeroes and
# assign Qty to indexes of Unit='KG'
df2['QtyKG'] = 0
df2['QtyKG'] = df2.loc[df2['Unit'] == 'KG','Qty']
# 3rd try: For all index values assing zero to QtyKG and then, using
# a FOR cycle, assign Qty value to QtyKG column for indexes where Unit
# is 'KG' managed as a list of index values.
df2.loc[:,'QtyKG'] = 0
for i in df2.loc[df2['Unit'] == 'KG'].index.to_list():
df2.loc[i, 'QtyKG'] = df2.loc[i, 'Qty']
# 4th try: For each index from 0 to len(df2) copy Qty to QtyKG if
# Unit is 'KG', else QtyKG is 0.
for i in range(len(df2)):
if df2.loc[i,'Unit'] == 'KG':
df2.loc[i,'QtyKG'] = df2.loc[i,'Qty']
else:
df2.loc[i,'QtyKG'] = 0
df2
它以某种方式工作,但我收到了以下错误:
C:\Users\arlain\AppData\Local\Temp\ipykernel_24992\3058135089.py:4:
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: https://pandas.pydata.org/
pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus
-a-copy
#1st df2['QtyKG'] = df['Qty'].copy()
df2.loc[df2['Unit'] != 'KG','QtyKG'] = 0
#2nd df2['QtyKG'] = 0
df2['QtyKG'] = df2.loc[df2['Unit'] == 'KG','Qty']
#3rd df2.loc[:,'QtyKG'] = 0
df2.loc[i, 'QtyKG'] = df2.loc[i, 'Qty']
#4th df2.loc[i,'QtyKG'] = df2.loc[i,'Qty']
df2.loc[i,'QtyKG'] = 0
最后,他们完成了这项工作,但我想知道一种完全避免错误/警告的方法以及正确的方法。
顺便说一句,问题 #64096923 #45170312 #60849563 问了类似的东西,但与我的情况不同。
提前非常感谢。
[我正在使用 Python 3.10.6 和 Pandas 2.1.2 ]
答: 暂无答案
评论