gpt4 book ai didi

python - 操作/重新编码 Pandas 数据框

转载 作者:行者123 更新时间:2023-12-04 08:53:44 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Pandas conditional creation of a series/dataframe column

(10 个回答)


12 个月前关闭。




在 Pandas 中进行一些简单的数据操作,但不确定如何按照下面定义的逻辑来做一些事情。我正在尝试根据此示例想法中 A 列中的值更改 C 列中的值。建议?

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
'B': [5, 6, 7, 8, 9],
'C': ['a', 'b', 'c', 'd', 'e']})
if df['A'] < 2:
df['C'] = "Small"
else:
df['C'] = "Big"

最佳答案

Pandas 的条件不同。你可以试试

import numpy as np
df['C'] = np.where(df.A < 2, 'small','big')
df
输出
   A  B      C
0 0 5 small
1 1 6 small
2 2 7 big
3 3 8 big
4 4 9 big

关于python - 操作/重新编码 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63957735/

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