假设我在pandas中编写此代码以创建数据帧:
[
{
"city": "Owosso",
"geohash": "dpshsfsytw8k",
"country": "US",
"county": "Shiawassee",
"state": "Michigan",
"state_short": "MI",
"postal_code": "48867",
"latitude": 42.9934,
"longitude": -84.1595,
"timezone": "America/Detroit"
}
]
这导致以下数据帧:
pd.DataFrame({'x':random.sample(range(1,100), 4),
'y':random.sample(range(1,100), 4),
'z':random.sample(range(1,100), 4)},
index = [['a1', 'b1', 'c1','d1'], ['a2', 'b2', 'c2', 'd2']])
我想通过传递一个列表来选择多索引行:
x y z
a1 a2 8 2 85
b1 b2 43 93 58
c1 c2 1 46 24
d1 d2 60 37 62
返回:
[[a1, a2], [b1, b2], [c1, c2]]
pandas中有一个函数吗?
答案 0 :(得分:3)
你非常接近:你需要将索引定义为元组列表而不是列表列表:
target_index = [('a1', 'a2'), ('b1', 'b2'), ('c1', 'c2')]
然后
df.loc[target_index]
为您提供所需的输出:
x y z
a1 a2 0 2 3
b1 b2 1 3 4
c1 c2 2 4 5
答案 1 :(得分:0)
您可以使用df.iloc
import pandas as pd
import random
df = pd.DataFrame({'x':random.sample(range(1,100), 4),
'y':random.sample(range(1,100), 4),
'z':random.sample(range(1,100), 4)},
index = [['a1', 'b1', 'c1','d1'], ['a2', 'b2', 'c2', 'd2']])
df.iloc[0:3]
返回
x y z
a1 a2 51 36 70
b1 b2 37 45 63
c1 c2 96 16 64