【问题标题】:Pandas Combining two columns based on whichever column has dataPandas 根据哪一列有数据组合两列
【发布时间】:2022-08-11 22:53:59
【问题描述】:

我试图根据值将两列组合成第三列。 原始数据

时间GearUp GearDn
1 南南 2 南下 3 南南 4 南南 5 UP NaN 6 南南 7 南南

要求的结果: 时间齿轮 1 南
2 下 3 下 4 下 5 上
6 上
7 上

我尝试使用 where 方法,但依次在每一列上使用,但第二次使用从第一次开始覆盖数据。

谢谢。

  • 请解释逻辑

标签: python pandas


【解决方案1】:

利用

df['Gear'] = df['GearUp'].combine_first(df['GearDown'])

请参阅此链接以获取文档 https://pandas.pydata.org/docs/reference/api/pandas.Series.combine_first.html

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2018-11-24
    • 2021-08-21
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多