Pandas Dataframe:在multiindex中选择列包含X的行

2024-09-30 22:26:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我正在努力处理这个数据,我有一个数据文件 约100行x 24列。在

我用

data = pd.read_csv(fileName, header=[0,1])
df = pd.DataFrame(data=data)

然后我只想处理其中的一部分,所以只选择了这些列:

^{2}$

问题:现在我想选择“QUESTNNR”中包含“PS”的行 我可以创建这个“列表”的真/假,但当我尝试使用它时,我得到:

onlyPS = postSurvey['QUESTNNR'] == 'PS'
postSurvey = postSurvey[onlyPS]
ValueError: cannot join with no level specified and no overlapping names

我得到了:

postSurvey.xs('PS', level='QUESTNNR')
AttributeError: 'RangeIndex' object has no attribute 'get_loc_level'

我尝试过stackoverflow和其他来源的各种解决方案,但需要帮助。在

数据帧:

A       B       C       D       E       F       G       H       Q       W       E       R       T       Y       U       I       O       P       S       J       K       L       Z       X       C       V       N       M       A1      A2      A3      S4      F4      G5
ASDF1   ASDF2   ASDF3   ASDF4   ASDF5   ASDF6   ASDF7   ASDF8   ASDF9   ASDF10  ASDF11  ASDF12  ASDF13  ASDF14  ASDF15  ASDF16  ASDF17  ASDF18  ASDF19  ASDF20  ASDF21  ASDF22  ASDF23  ASDF24  ASDF25  ASDF26  ASDF27  ASDF28  ASDF29  ASDF30  ASDF31  ASDF32  ASDF33  ASDF34
138     PS  interview   eng     date                                                        10  2   5   7   2012    10  1   13  1   26  1   0   1   1
129     QB2 interview   eng     date    4   6   5       56,10,34,7,20                                                                           1   0   2   2
130     QC1 interview   eng     date    6   2   6           7                                                                       1   0   2   2
131     QD2 interview   eng     date    3   8   6               5,8,15                                                                  1   0   2   2

Tags: csv数据noreaddatadate数据文件level