我有以下多索引数据框:
from io import StringIO
import pandas as pd
datastring = StringIO("""File,no,runtime,value1,value2
A,0, 0,12,34
A,0, 1,13,34
A,0, 2,23,34
A,1, 6,23,38
A,1, 7,22,38
B,0,17,15,35
B,0,18,17,35
C,0,34,23,32
C,0,35,21,32
""")
df = pd.read_csv(datastring, sep=',')
df.set_index(['File','no',df.index], inplace=True)
>> df
runtime value1 value2
File no
A 0 0 0 12 34
1 1 13 34
2 2 23 34
1 3 6 23 38
4 7 22 38
B 0 5 17 15 35
6 18 17 35
C 0 7 34 23 32
8 35 21 32
我想要得到的只是带有新文件和不同数字的每个条目的第一个值
A 0 34
A 1 38
B 0 35
C 0 32
我能找到的最相似的问题在哪里
仅知道结果测量计数的对 pandas 数据框进行重新采样 https://stackoverflow.com/questions/20107958/resample-pandas-dataframe-only-knowing-result-measurement-count/20108446#20108446
pandas 中基于 MultiIndex 的索引 https://stackoverflow.com/questions/14964493/multiindex-based-indexing-in-pandas
在 pandas MultiIndex DataFrame 中选择行 https://stackoverflow.com/questions/53927460/select-rows-in-pandas-multiindex-dataframe
但我无法从它们中构建解决方案。我得到的最好的是ix
操作,但由于技术上的值仍然存在(只是不显示),结果是
idx = pd.IndexSlice
df.loc[idx[:,0],:]
例如,可以过滤0
值,但仍会返回数据帧的整个其余部分。
多重索引是否是完成当前任务的正确工具?怎么解决这个问题呢?