[英]Pandas dataframe get columns names and value_counts
how to get all column names where values in columns are 'f' or 't' into array ? 如何获取所有列名称为“ f”或“ t”的列名称到数组中?
df['FTI'].value_counts()
instead of this 'FTI' i need array of returned columns. 代替此“ FTI”,我需要返回列的数组。 Is it possible?
可能吗?
Reproducible example: 可重现的示例:
df = pd.DataFrame({'col1':[1,2,3], 'col2':['f', 'f', 'f'], 'col3': ['t','t','t'], 'col4':['d','d','d']})
col1 col2 col3 col4
0 1 f t d
1 2 f t d
2 3 f t d
Such that, using eq
and all
: 这样,使用
eq
和all
:
>>> s = (df.eq('t') | df.eq('f')).all()
col1 False
col2 True
col3 True
col4 False
dtype: bool
To get the names: 获取名称:
>>> s[s].index.values
array(['col2', 'col3'], dtype=object)
To get the positions: 获取职位:
>>> np.flatnonzero(s) + 1
array([2, 3])
Yes. 是。 It is possible.
有可能的。 Here is one way
这是一种方法
You can get the columns like this. 您可以得到像这样的列。
cols=[]
for col in df.columns:
if df[col].str.contains('f|t').any()==True:
cols.append(col)
Then you can just use this for frequencies 然后您可以将其用于频率
f= pd.Series()
for col in cols:
f=pd.concat([f,df[col].value_counts()])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.