繁体   English   中英

Pandas 将列与特定值进行比较

[英]Pandas comparing columns to specific values

我创建了一个用字符串填充的 dataframe。“日期”值是唯一的,其他列有重复的内容

d = {'Date':['1','2','3','4','5'],
     'col1':['a','a','b','b','b','e'],
     'col2':['c','c','c','c','d','f']}
df = pd.DataFrame(data=d)
print(df)
    Day  col1  col2
0     1     a     c
1     2     a     c
2     3     b     c
3     4     b     c
4     5     b     d
5     6     e     f

我想选择一行并测试它的值在所有剩余行中重复了多少。 我想出的唯一逻辑测试总是返回 False。

chosen = df.loc[df['Date'] == '3']
print(chosen)
      Day   col1  col2
2     3     b     c

df['Result'] = 0
for i in chosen.columns[1:]:
    print(i)
    df['Result'] += np.where(df[i].equals(chosen[i]),1,0)
print(df)
    Day  col1  col2   Result
0     1     a     c     0
1     2     a     c     0
2     3     b     c     0
3     4     b     c     0
4     5     b     d     0
5     6     e     f     0

预期 output:

     Day  col1  col2   Result
0     1     a     c     1
1     2     a     c     1
2     3     b     c     2
3     4     b     c     1
4     5     b     d     1
5     6     e     f     0

实现这一目标的最佳方法是什么? 我可以创建只有“选择”值的列,然后逐列比较,但我猜这种方法对于几十列和几千行来说会很慢。

你只需要这一行:

chosen = len(df.loc[df['Date'] == '3'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM