【问题标题】:How to separate timestamp data in data frame by using read_sql?如何使用 read_sql 分离数据框中的时间戳数据?
【发布时间】:2019-04-10 01:20:08
【问题描述】:

我有一个如下的 SQLite 表:

Date                              Value
01-01-2018 00:00:00               12,2
02-01-2018 00:00:00               13,5
03-01-2018 00:00:00               15,6
04-01-2018 00:00:00               17,8
05-01-2018 00:00:00               18,3

我可以使用pandas.read_sql(“select * from TableName”, conn) 阅读此表。

我想像这样将它们写入 csv 文件:

Year;month;day;hour;minute;second;value
01;01,2018;00;00;00;12,2
02;01;2018;00;00;00;13,5
03;01;2018;00;00;00;15,6
04;01;2018;00;00;00;17,8
05;01;2018;00;00;00;18,3

我也调查了所有示例和问题以及 pandas 文档。但是我找不到像这样将表格转换为 csv 的解决方案。

日期是时间戳,值是 SQLite 中的真实类型。

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    我通过考虑that 来使用该功能的答案:

    df = pandas.read_sql("SELECT * FROM tableName", conn)
    df['Date'] = pandas.to_datetime(df['Tarih'])
    df['Year'] = [d.year for d in df['Tarih']]
    df['Month'] = [d.month for d in df['Tarih']]
    df['Day'] = [d.day for d in df['Tarih']]
    df['Hour'] = [d.hour for d in df['Tarih']]
    df['Minute'] = [d.minute for d in df['Tarih']]
    df['Second'] = [d.second for d in df['Tarih']]
    df['Data'] = [float(d) for d in df['Value']]
    df.drop(columns=["Date", "Value"], inplace=True)
    df.to_csv(csvPath, sep=";", index=False)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 2021-03-10
      相关资源
      最近更新 更多