gpt4 book ai didi

python - 从 Pandas 数据框中选择特定的索引、列对

转载 作者:行者123 更新时间:2023-12-03 13:52:16 25 4
gpt4 key购买 nike

我有一个数据框 x:

x = pd.DataFrame(np.random.randn(3,3), index=[1,2,3], columns=['A', 'B', 'C'])
x


A B C
1 0.256668 -0.338741 0.733561
2 0.200978 0.145738 -0.409657
3 -0.891879 0.039337 0.400449
我想选择一堆索引列对来填充新系列。例如,我可以选择 [(1, 'A'), (1, 'B'), (1, 'A'), (3, 'C')]这将生成一个包含 4 个元素的列表或数组或系列:
[0.256668, -0.338741, 0.256668, 0.400449]
知道我应该怎么做吗?

最佳答案

我想 get_value()lookup()是比较快的:

import numpy as np
import pandas as pd
x = pd.DataFrame(np.random.randn(3,3), index=[1,2,3], columns=['A', 'B', 'C'])

locations = [(1, "A"), (1, "B"), (1, "A"), (3, "C")]

print x.get_value(1, "A")

row_labels, col_labels = zip(*locations)
print x.lookup(row_labels, col_labels)

关于python - 从 Pandas 数据框中选择特定的索引、列对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819994/

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