gpt4 book ai didi

python - Pandas :将列的值分配到字典值设置的限制

转载 作者:行者123 更新时间:2023-11-28 20:36:24 25 4
gpt4 key购买 nike

如何删除 iterrows()?这可以用 numpy 或 pandas 更快地完成吗?

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
'B': 'one one two three two two one three'.split(),
'C': np.arange(8)*0 })
print(df)
# A B C
# 0 foo one 0
# 1 bar one 0
# 2 foo two 0
# 3 bar three 0
# 4 foo two 0
# 5 bar two 0
# 6 foo one 0
# 7 foo three 0

selDict = {"foo":2, "bar":3}

这个有效:

for i, r in df.iterrows():
if selDict[r["A"]] > 0:
selDict[r["A"]] -=1
df.set_value(i, 'C', 1)

print df
# A B C
# 0 foo one 1
# 1 bar one 1
# 2 foo two 1
# 3 bar three 1
# 4 foo two 0
# 5 bar two 1
# 6 foo one 0
# 7 foo three 0

最佳答案

如果我没理解错,你可以使用 cumcount:

df['C'] = (df.groupby('A').cumcount() < df['A'].map(selDict)).astype('int')

df
Out:
A B C
0 foo one 1
1 bar one 1
2 foo two 1
3 bar three 1
4 foo two 0
5 bar two 1
6 foo one 0
7 foo three 0

关于python - Pandas :将列的值分配到字典值设置的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45132656/

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