python - 根据行条件对轴 1 上的数据框进行子集化

标签 python pandas dataframe

df = DataFrame({'A':['Cat had a nap','Dog had puppies','Did you see a Donkey','kitten got angry','puppy was cute'],'Cat':[1,0,0,1,0],'Dog':[0,1,0,0,1]})


              A         Cat Dog
0   Cat had a nap        1  0
1   Dog had puppies      0  1
2   Did you see a Donkey 0  0
3   kitten got angry     1  0
4   puppy was cute       0  1

编辑1: 如何使用该行中包含“1”的串联列名映射每一行?

预期输出:

             A         Cat Dog Category
0   Cat had a nap        1  0   Cat, Dog
1   Dog had puppies      0  1   Dog
2   Did you see a Donkey 0  0   NaN
3   kitten got angry     1  0   Cat, Dog
4   puppy was cute       0  1   Dog

最佳答案

比较eq DataFrame 的所有值,并按 any 检查每行列至少一个 True :

对于过滤器行:

df = df[df.eq(1).any(axis=1)]
print (df)
                  A  Cat  Dog
0     Cat had a nap    1    0
1   Dog had puppies    0    1
3  kitten got angry    1    0
4    puppy was cute    0    1

对于过滤列:

df = df.loc[:, df.eq(1).any()]
print (df)
   Cat  Dog
0    1    0
1    0    1
2    0    0
3    1    0
4    0    1

对于过滤列和行:

m = df.eq(1)
df = df.loc[m.any(axis=1), m.any()]
print (df)
   Cat  Dog
0    1    0
1    0    1
3    1    0
4    0    1

编辑:

df['Category'] = df.eq(1).dot(df.columns + ',').str[:-1]
print (df)
                      A  Cat  Dog Category
0         Cat had a nap    1    0      Cat
1       Dog had puppies    0    1      Dog
2  Did you see a Donkey    0    0         
3      kitten got angry    1    0      Cat
4        puppy was cute    0    1      Dog

关于python - 根据行条件对轴 1 上的数据框进行子集化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996410/

相关文章:

Pandas Multiple "group by"和对值的操作

python - Pandas:反转具有相同值的行

python - 如何通过列表中的位置从下拉菜单中选择选项?

python - 如何在没有 __dict__ 的情况下创建类对象

pandas - 为什么 Pandas df.to_hdf ("a_file", "a_key") 多次执行时输出大小会增加

python - 从特定日期的 pandas DataFrame 中选择行

python - 根据 pandas 中的情况替换某些值

python - 我如何计算滚动 idxmax

python - 如何使用Python删除csv文件中只有一项的行?

追加空列表时应为空的 Python 列表