Python 是否可以访问熊猫中给定列名称列表的数据?
我的数据框如下所示:Python 是否可以访问熊猫中给定列名称列表的数据?,python,pandas,numpy,Python,Pandas,Numpy,我的数据框如下所示: foo bar buzz 0 0.1 0.2 0.3 1 0.4 0.5 0.6 2 0.7 0.8 0.9 现在我有了一个列表['foo'、'bar'、'buzz'] 我想在正确的行位置获得正确的元素,结果可以是一个系列或类似[0.1,0.5,0.9] 我怎样才能做到这一点呢?只要写下: Dataframe[[here the list of columns]] 它将为您提供列的所有值,否则您必须使用for循环或列表理解进行迭代,如
foo bar buzz
0 0.1 0.2 0.3
1 0.4 0.5 0.6
2 0.7 0.8 0.9
现在我有了一个列表['foo'、'bar'、'buzz']
我想在正确的行位置获得正确的元素,结果可以是一个系列或类似[0.1,0.5,0.9]
我怎样才能做到这一点呢?只要写下:
Dataframe[[here the list of columns]]
它将为您提供列的所有值,否则您必须使用for循环或列表理解进行迭代,如下所示:
[row for row in dataframe[here the column names]]
只要写下:
Dataframe[[here the list of columns]]
它将为您提供列的所有值,否则您必须使用for循环或列表理解进行迭代,如下所示:
[row for row in dataframe[here the column names]]
您正在寻找:
或使用列表:
[df.loc[e,i] for e,i in enumerate(l)]
#or [df.iloc[e,df.columns.get_loc(i)] for e,i in enumerate(l)]
#[0.1, 0.5, 0.9]
您正在寻找:
或使用列表:
[df.loc[e,i] for e,i in enumerate(l)]
#or [df.iloc[e,df.columns.get_loc(i)] for e,i in enumerate(l)]
#[0.1, 0.5, 0.9]
您是否尝试过
df[['foo'、'bar'、'buzz']
?df.lookup(*zip(*enumerate(l))
您是否尝试过df[[foo'、'bar'、'buzz']
?df.lookup(*zip(*enumerate(l))