gpt4 book ai didi

python - 来自具有列名称的列的 Pandas 系列

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:40 25 4
gpt4 key购买 nike

我喜欢从包含列名称的列中获取新系列,如下所示:

df = pd.DataFrame({'Col':['a','b','c','c','a','a'], 'a': [20,10,5,1,1,10], 'b': [4,10,5,2,10,12], 'c': [20,20,15,4,8,19]})

>>> df

Col a b c
0 a 20 4 20
1 b 10 10 20
2 c 5 5 15
3 c 1 2 4
4 a 1 10 8
5 a 10 12 19

我需要一个系列,它显示每一行“Col”列中的值:

>>> s

0 20
1 10
2 15
3 4
4 1
5 10
dtype: int64

非常感谢您的帮助!

最佳答案

您可以对数据框使用apply

In [339]: df.apply(lambda x: x[x['Col']], axis=1)
Out[339]:
0 20
1 10
2 15
3 4
4 1
5 10
dtype: int64

关于python - 来自具有列名称的列的 Pandas 系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36702077/

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