我正在尝试根据多层列来筛选行。我不能确定我的数据是否有错误
df[df[[('description','type')]].isin(["Filter1","Filter2"])]
>> TypeError: '<' not supported between instances of 'str' and 'float'如果我尝试转换为字符串,我得到相同的错误
df.loc[:,[('description','type')]] = df.loc[:,[('description','type')]].astype(str)
>> TypeError: '<' not supported between instances of 'str' and 'float'数据(包含2列的前5行):
description
type date
0 Filter 1 1 Mth-7/21
1 Filter 1 1 Yr-7/21
2 Filter 2 10 Yr-7/21
3 Filter 2 15 Yr-7/21
4 Filter 3 2 Yr-7/21
5 Filter 3 3 Mth-7/21当我认为我所有的列值都是字符串时,我不确定为什么它总是抛出前两个错误
我脑海中的一个简单的例子:
df1 = pd.DataFrame({('description','type'):['a','b','c','d'],
('description','value'):[1,2,3,4]})
df1[df1[('description','type')].isin(['a','b'])]发布于 2021-08-29 20:48:11
您的两个筛选器之间存在差异:
df[df[[('description','type')]].isin(["Filter1","Filter2"])]
# ^------------------------^--- HERE
df1[df1[('description','type')].isin(['a','b'])]# First case: float appears
>>> df1[df1[[('description','type')]].isin(['a','b'])]
description
type value
0 a NaN
1 b NaN
2 NaN NaN
3 NaN NaN
# Second case
>>> df1[df1[('description','type')].isin(['a','b'])]
description
type value
0 a 1
1 b 2https://stackoverflow.com/questions/68976691
复制相似问题