【问题标题】:how to find max of a columns with same name如何查找具有相同名称的列的最大值
【发布时间】:2021-02-05 14:07:27
【问题描述】:

我在使用此数据框时遇到了一些问题,其中必须将具有相同名称的列缩减为至少一个“1”作为“1”的值。

+---+---+---+---+---+---+---+---+---+
| a | a | a | b | c | c | c | d | d |
+---+---+---+---+---+---+---+---+---+
| 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 0 |
| 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 |
+---+---+---+---+---+---+---+---+---+

对于这样的事情,对大型数据集的每一列使用“或”条件可能是一项耗时的任务,所以我很难弄清楚。我用了 max(axis=1, level=0) 还是不行。

我想要的输出:

+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | 1 | 1 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 |
+---+---+---+---+

【问题讨论】:

    标签: python pandas numpy dataframe data-manipulation


    【解决方案1】:

    检查max

    df = df.max(level=0, axis=1)
    

    【讨论】:

    • 嘿,感谢您的回复,正如我上面提到的,我已经尝试过了,但我收到错误消息“None of [Int64Index([ 0, 1, 2, ......... .], dtype='int64', length=30507)] 在 [columns]" 中,我很难弄清楚。主要是因为我有大量数据。
    • @PraneethBhandary 你的专栏还不是专栏,可以做 df.head().to_dict() 吗?
    • 我收到一条警告消息,说当我尝试使用 df.head().to_dict() 时我的某些列被省略了
    • 嘿,我想通了,我的数据被编码为 1 和 0,因为它是我使用的对象类型 .astype('int').max(level=0, axis=1)它给了我输出。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多