-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo3.py
49 lines (39 loc) · 1.54 KB
/
demo3.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pandas as pd
import os
df = pd.read_pickle(os.path.join('/Users/amandashay/Documents/corepy', 'data_frame.pickle'))
# Smaller object for easier vis
small_df = df.iloc[49980:50019, :].copy()
# Basic Excel
small_df.to_excel("basic.xlsx")
small_df.to_excel("no_index.xlsx", index=False)
small_df.to_excel("columns.xlsx", columns=["artist","title","year"])
# Multiple worksheets
writer = pd.ExcelWriter('multiple_sheets.xlsx', engine='xlsxwriter')
small_df.to_excel(writer, sheet_name="Preview",index=False)
df.to_excel(writer, sheet_name="Complete", index=False)
writer.save()
# Conditional formatting
artist_counts =df['artist'].value_counts()
artist_counts.head()
writer = pd.ExcelWriter('colors.xlsx', engine='xlsxwriter')
artist_counts.to_excel(writer,sheet_name="Artist Counts")
sheet = writer.sheets['Artist Counts']
cells_range = 'B2:B{}'.format(len(artist_counts.index))
sheet.conditional_format(cells_range, {'type': '2_color_scale',
'min_value':'10',
'min_type': 'percentile',
'max_values':'99',
'max_type': 'percentile'})
writer.save()
#SQL
import sqlite3
with sqlite3.connect('my_database.db' )as conn:
small_df.to_sql('Tate', conn)
import sqlalchemy as sa
# with sa.create_engine('postgressql://localhost/my_data) as conn:
# small_df.to_sql('Tate', conn)
#JSON
small_df.to_json('default.json')
small_df.to_json('table.json',orient='table')