从pandas数据框中选择特定的索引,列对 [英] Select specific index, column pairs from pandas dataframe

查看:105
本文介绍了从pandas数据框中选择特定的索引,列对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框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

,我想选择一堆索引列对来填充新的Series.例如,我可以选择[(1,A),(1,B),(1,A),(3,C)],它会生成一个包含4个元素的列表或数组或序列:

and I would like to select a bunch of index column pairs to populate a new Series. For example, I could select [(1, A), (1, B), (1, A), (3, C)] which would generate a list or array or series with 4 elements:

[0.256668, -0.338741, 0.256668, 0.400449]

关于我该怎么做的任何想法?

Any idea of how I should do that?

推荐答案

我认为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)

这篇关于从pandas数据框中选择特定的索引,列对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆