鱼C论坛

 找回密码
 立即注册
查看: 1865|回复: 3

[已解决]如何修改才能消除这警告,求大师帮忙,谢谢

[复制链接]
发表于 2023-4-18 08:30:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
警告内容如下:

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
  filtered_df['B'] = filtered_df['B'].astype(int)
最大值: 12
对应的日期: 2022-12-15

运行的代码:
import pandas as pd

data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df = pd.DataFrame(data, columns=columns)

# 将日期列转换为日期格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022-12-13的行
filtered_df = df[df['日期'] > '2022-12-13']

# 将B列转换为整数格式
filtered_df['B'] = filtered_df['B'].astype(int)

# 找到B列的最大值
max_value = filtered_df['B'].max()

# 找到对应最大值的日期
max_date = filtered_df.loc[filtered_df['B'] == max_value, '日期'].iloc[-1]

print("最大值:", max_value)
print("对应的日期:", max_date.strftime('%Y-%m-%d'))
最佳答案
2023-4-18 09:04:32
futui 发表于 2023-4-18 08:41
新的提示:
: DeprecationWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set ...


为了解决DeprecationWarning,请使用df[df.columns[i]] = newvals替代.loc[]方法。以下是修改后的代码:
import pandas as pd

data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df = pd.DataFrame(data, columns=columns)

# 将日期列转换为日期格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022-12-13的行
filtered_df = df[df['日期'] > '2022-12-13'].copy()

# 将B列转换为整数格式
filtered_df[filtered_df.columns[4]] = filtered_df['B'].astype(int)

# 找到B列的最大值
max_value = filtered_df['B'].max()

# 找到对应最大值的日期
max_date = filtered_df.loc[filtered_df['B'] == max_value, '日期'].iloc[-1]

print("最大值:", max_value)
print("对应的日期:", max_date.strftime('%Y-%m-%d'))


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-18 08:34:44 | 显示全部楼层
警告里有这一句提示:Try using .loc[row_indexer,col_indexer] = value instead

故您可以使用.loc[row_indexer,col_indexer] = value来修改数据,以消除SettingWithCopyWarning警告。请参考以下修改后的代码:
import pandas as pd

data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df = pd.DataFrame(data, columns=columns)

# 将日期列转换为日期格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022-12-13的行
filtered_df = df[df['日期'] > '2022-12-13'].copy()

# 将B列转换为整数格式
filtered_df.loc[:, 'B'] = filtered_df['B'].astype(int)

# 找到B列的最大值
max_value = filtered_df['B'].max()

# 找到对应最大值的日期
max_date = filtered_df.loc[filtered_df['B'] == max_value, '日期'].iloc[-1]

print("最大值:", max_value)
print("对应的日期:", max_date.strftime('%Y-%m-%d'))

请注意,在筛选出新的DataFrame时,使用copy()方法来创建新的DataFrame副本,以避免在原始DataFrame上进行修改。在将'B'列转换为整数格式时,我们使用.loc[]方法。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-18 08:41:26 | 显示全部楼层
新的提示:
: DeprecationWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set the values inplace instead of always setting a new array. To retain the old behavior, use either `df[df.columns[i]] = newvals` or, if columns are non-unique, `df.isetitem(i, newvals)`
  filtered_df.loc[:, 'B'] = filtered_df['B'].astype(int)
最大值: 12
对应的日期: 2022-12-15
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-18 09:04:32 | 显示全部楼层    本楼为最佳答案   
futui 发表于 2023-4-18 08:41
新的提示:
: DeprecationWarning: In a future version, `df.iloc[:, i] = newvals` will attempt to set ...


为了解决DeprecationWarning,请使用df[df.columns[i]] = newvals替代.loc[]方法。以下是修改后的代码:
import pandas as pd

data = [["004243","C","2022-12-22",'',"4"],["162719","A","2022-12-20","8","6"],
       ["004243","C","2022-12-19",'',"12"],["519212","B","2022-12-18","56","3"],
       ["162411","E","2022-12-17","8","7"],["007844","FC","2022-12-16","","8"],
       ["163208","G","2022-12-15","66","12"],["162719","A","2022-12-14","8","6"],
        ["519212","B","2022-12-12","56","3"],["162411","E","2022-12-11","8","7"],
        ["007844","FC","2022-12-10","","18"],["163208","G","2022-12-8","66","12"]]
columns = ["编号","名称","日期","A","B"]
df = pd.DataFrame(data, columns=columns)

# 将日期列转换为日期格式
df['日期'] = pd.to_datetime(df['日期'])

# 筛选日期大于2022-12-13的行
filtered_df = df[df['日期'] > '2022-12-13'].copy()

# 将B列转换为整数格式
filtered_df[filtered_df.columns[4]] = filtered_df['B'].astype(int)

# 找到B列的最大值
max_value = filtered_df['B'].max()

# 找到对应最大值的日期
max_date = filtered_df.loc[filtered_df['B'] == max_value, '日期'].iloc[-1]

print("最大值:", max_value)
print("对应的日期:", max_date.strftime('%Y-%m-%d'))


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-15 04:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表