-
Notifications
You must be signed in to change notification settings - Fork 1
/
pivot_table.py
25 lines (18 loc) · 799 Bytes
/
pivot_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding: utf-8 -*-
import pandas as pd
FILE_TO_WRITE = "./sample_files/pivot.csv" #save pivot table data to pivot.csv
def generate_pivot_table(FILE_TO_READ):
try:
df = pd.read_csv(FILE_TO_READ)
df['Date'] = df._time.apply(lambda x: x[:10]) #For each row in _time column, extract the date value
df['Time'] = df._time.apply(lambda x: x[11:19]) #For each row in _time column, extract the time value
pivot = df.pivot(index='Date', columns='Time', values='Count')
pivot.to_csv(FILE_TO_WRITE, index=True)
#print(pivot)
except Exception as e:
raise e
def main():
FILE_TO_READ = "./sample_files/pivot_data.csv" #data file
generate_pivot_table(FILE_TO_READ)
if __name__ == "__main__":
main()