0

I have a SQLite table like below:

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

I can read this table with pandas.read_sql(“select * from TableName”, conn).

I want to write them into csv file like that:

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

I have investigated all examples and questions and pandas documentation, as well. However I could not find a solution to convert my table to csv like that.

Date is timestamp and value is real type in SQLite.

Malik Asad
  • 441
  • 4
  • 15
Mustafa Uçar
  • 442
  • 1
  • 6
  • 18

1 Answers1

0

I use that function by considering that answer:

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)
Mustafa Uçar
  • 442
  • 1
  • 6
  • 18