-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpandas_functions.py
103 lines (78 loc) · 2.65 KB
/
pandas_functions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from sheetFeeder import dataSheet
import pandas as pd
# import numpy as np
# This code shows how to translate to and from the Popular Pandas dataframe object type. You can use sheetFeeder to easily import your data from a Google Sheet into Pandas and/or write out your processed data back to Google Sheets. Installation of Pandas is required.
def main():
# Test sheet with sample data
sheet_id = '19zHqOJt9XUGfrfzAXzOcr4uARgCGbyYiOtoaOCAMP7s'
sheet_range = 'Sheet1!A:Z'
# Data from sheetFeeder dataSheet
print("1. dataSheet data array:")
ds = dataSheet(sheet_id, sheet_range).getData()
print(ds)
print("")
# ds to df example
df = datasheet_to_dataframe(sheet_id, sheet_range)
print("2. Converted to DataFrame:")
print(df)
print("")
print("DataFrame shape:")
print(df.shape)
print("")
print("Data types:")
print(df.dtypes)
print("")
print("Column averages:")
print(df.mean())
# df['mean'] = df.mean(numeric_only=True, axis=1)
# print(df)
# # df.assign(mean_a=df.a.mean(), mean_b=df.b.mean())
# ds = dataframe_to_datasheet(df)
# # print(ds)
# dataSheet(sheet_id, sheet_range).appendData(ds)
# quit()
print("")
# df back to ds
ds = dataframe_to_datasheet(df)
print("3. Converted back to dataSheet array:")
print(ds)
print("")
# Get sheetFeeder data as series, and convert to Pandas df
ds = dataSheet(sheet_id, sheet_range)
ds_series = ds.getDataSeries()
print("4. Data as series:")
print(ds_series)
print("")
df = pd.DataFrame(ds_series)
print("5. Series converted to dataframe:")
print(df)
# Functions to translate sheetFeeder dataSheet to/from Pandas dataframes
def datasheet_to_dataframe(_sheet_id, _sheet_range):
# Read sheet data into a Pandas dataframe object.
the_data = dataSheet(_sheet_id, _sheet_range).getData()
the_data = numberize_data(the_data)
heads = the_data.pop(0) # assumes the first row is column heads.
return pd.DataFrame(the_data, columns=heads)
def dataframe_to_datasheet(_df):
heads = list(_df.columns.values)
ds = _df.values.tolist()
ds.insert(0, heads)
return ds
def cast_to_number(string):
try:
result = int(string)
except ValueError:
try:
result = float(string)
except ValueError:
result = string
return result
def numberize_data(array):
# input is a list of lists;
# output casts any number-like strings as either integers or floats
result = []
for row in array:
result.append([cast_to_number(r) for r in row])
return result
if __name__ == "__main__":
main()