【问题标题】:Creating new data frame based on a column unique value基于列唯一值创建新数据框
【发布时间】:2021-09-26 21:23:19
【问题描述】:

我有一个如下的df:-

StartTime Location Sales
2021-09-22 00:00:00 New York 10
2021-09-22 00:00:00 Delhi 20
2021-09-22 00:05:00 New York 10
2021-09-22 00:05:00 Delhi 20
2021-09-23 00:00:00 New York 10
2021-09-23 00:00:00 Delhi 20
2021-09-23 00:05:00 New York 10
2021-09-23 00:05:00 Delhi 20

我已将日期从“开始时间”列中分离出来

    df['dateSeparate'] = df['StartTime'].str.extract('(\d\d\d\d-\d\d-\d\d)', expand=False).str.strip()
print(df)

新的df看起来像

StartTime Location Sales dateSeparate
2021-09-22 00:00:00 New York 10 2021-09-22
2021-09-22 00:00:00 Delhi 20 2021-09-22
2021-09-22 00:05:00 New York 10 2021-09-22
2021-09-22 00:05:00 Delhi 20 2021-09-22
2021-09-23 00:00:00 New York 10 2021-09-23
2021-09-23 00:00:00 Delhi 20 2021-09-23
2021-09-23 00:05:00 New York 10 2021-09-23
2021-09-23 00:05:00 Delhi 20 2021-09-23

我可以知道有什么方法可以根据“dateSeparate”中的不同日期创建新的数据框吗?

提前谢谢你。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    试试这个:

    df['StartTime'] = pd.to_datetime(df.StartTime) # if value in StartTime is string use this line.
    
    df['dateSeparate'] = df['StartTime'].dt.strftime('%y-%m-%d')
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 2020-10-11
      • 2020-10-01
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多