从DataFrame中按行提取列名称到Series [英] Row-wise extract column names from DataFrame into Series
本文介绍了从DataFrame中按行提取列名称到Series的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将列表中的列名提取到按每行值过滤的系列中
I'd like to extract the column names in a list to a Series filtered on the values in each row
In [1]: import pandas as pd
In [2]: df =pd.DataFrame({'colA':[1,0,1], 'colB':[0,0,1], 'colC':[1,0,0]})
In [3]: print(df)
colA colB colC
0 1 0 1
1 0 0 0
2 1 1 0
生成的系列应如下所示:
The resulting Series should look like this:
0 [colA, colC]
1 []
2 [colA, colB]
dtype: object
这是我想出的受折磨的解决方案:
Here's the tortured solution I came up with:
In [4]: df2 = df.T
In [5]: l = [df2[df2[i]>0].index.values.tolist() for i in range(3)]
In [6]: print(pd.Series(l))
0 [colA, colC]
1 []
2 [colA, colB]
dtype: object
这样做有没有那么折磨的方式?
Is there a less tortured way of doing this?
推荐答案
您可以使用 np.where
假设您的数据帧由0和1组成,并根据结果创建一个Series:
You could use np.where
assuming your dataframe is constituted by 0's and 1's, and create a Series from the result:
x = np.where(df,df.columns,'')
pd.Series([' '.join(i).split() for i in x])
0 [colA, colC]
1 []
2 [colA, colB]
这篇关于从DataFrame中按行提取列名称到Series的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文