【问题标题】:How can I do the following dataframe transformation in Python?如何在 Python 中进行以下数据框转换?
【发布时间】:2022-01-10 22:41:33
【问题描述】:

有人可以帮助我使用 PYTHON 函数转换下表吗?

我需要 2 个新列:一个“关注者类型”,其中包含自然或付费条目,以及一个“关注者计数”,其值对应于关注者类型。

当前表 -

org organic follower paid follower start date stop date
One 2 0 1634169600000 1634256000000
One -1 0 1634256000000 1634342400000

想要的表 -

org start date stop date Follower Type Follower Count
One 1634169600000 1634256000000 Organic 2
One 1634169600000 1634256000000 Paid 0
One 1634256000000 1634342400000 Organic -1
One 1634256000000 1634342400000 Paid 0

如果有人知道怎么做,请告诉我。 谢谢和干杯!

【问题讨论】:

    标签: python pandas dataframe etl


    【解决方案1】:

    使用重新索引来更改列顺序 ''' column_names = [“C”,“A”,“B”] df = df.reindex(columns=column_names)

    ''' 如下所示,您可以将列添加到现有数据框

    df[newcolumn]=公式

    【讨论】:

    • 我不想更改列顺序。我想在现有列上添加新列基础条件
    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 2017-01-21
    • 2016-06-12
    • 1970-01-01
    • 2018-08-11
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多