我有以下包含100列的DF1 (col_1
到col_100
),并且我想要设置一个条件,仅对列Item
中的每一项计数值> 50的列(col_1
到col_100
)。在Excel中,使用函数COUNTIF()
相当简单。我们如何在Python中实现同样的目标呢?
Item col_1 col_2 col_3 col_4 col_5 col_6 col_7 …
item_1 10 5 6 9 4 6 77 …
item_2 3 5 66 76 7 89 33 …
在上表中,结果应该是:
Item Result
item_1 1
item_2 3
发布于 2019-02-13 05:26:02
我会和DataFrame.transpose()一起做的
>>>df
Item col_1 col_2 col_3 col_4 col_5 col_6 col_7 …
item_1 10 5 6 9 4 6 77 …
item_2 3 5 66 76 7 89 33 …
df1 = df.T
tcol = []
for col in df1.columns:
tcol.append(len(df1.index[df1[col] > 50].tolist()))
df_total = pd.DataFrame(tcol, columns=['total'])
df = df.join(df_total)
P.S.也许有一种方法可以逐行计算条件和,但我一点也不知道。如果任何知情人士能指出如何使用,我将不胜感激!
https://stackoverflow.com/questions/54640508
复制相似问题