gpt4 book ai didi

pandas - 一次设置多列给出错误 "Not in index error!"

转载 作者:行者123 更新时间:2023-12-05 05:32:34 25 4
gpt4 key购买 nike

import pandas as pd
df = pd.DataFrame(
[
[5, 2],
[3, 5],
[5, 5],
[8, 9],
[90, 55]
],
columns = ['max_speed', 'shield']
)

df.loc[(df.max_speed > df.shield), ['stat', 'delta']] \
= 'overspeed', df['max_speed'] - df['shield']

我如上所述使用 .loc 设置多列,在某些情况下我得到 Not in index error!。我在上面做错了什么吗?

最佳答案

在用重复标量overspeed减去之后,创建大小相同的元组列表,如True的数量过滤系列:

m = (df.max_speed > df.shield)
s = df['max_speed'] - df['shield']

df.loc[m, ['stat', 'delta']] = list(zip(['overspeed'] * m.sum(), s[m]))
print(df)
max_speed shield stat delta
0 5 2 overspeed 3.0
1 3 5 NaN NaN
2 5 5 NaN NaN
3 8 9 NaN NaN
4 90 55 overspeed 35.0

帮助程序 DataFrame 的另一个想法:

df.loc[m, ['stat', 'delta']] = pd.DataFrame({'stat':'overspeed', 'delta':s})[m]

详细信息:

print(list(zip(['overspeed'] * m.sum(), s[m])))
[('overspeed', 3), ('overspeed', 35)]

print (pd.DataFrame({'stat':'overspeed', 'delta':s})[m])
stat delta
0 overspeed 3
4 overspeed 35

最简单的就是分别赋值:

df.loc[m, 'stat'] = 'overspeed'
df.loc[m, 'delta'] = df['max_speed'] - df['shield']

print(df)
max_speed shield stat delta
0 5 2 overspeed 3.0
1 3 5 NaN NaN
2 5 5 NaN NaN
3 8 9 NaN NaN
4 90 55 overspeed 35.0

关于pandas - 一次设置多列给出错误 "Not in index error!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74054978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com