Read Data
Clipboard
df = pd.read_clipboard()
กำหนดชื่อ Column เอง
n = ['No', 'County', 'Currency', 'Currency Code', 'Export', 'Import']
df = pd.read_clipboard(header=None,names=n)
SQL
import sqlite3
import random
import pandas as pd
import matplotlib.pyplot as plt
def create_table(db):
try:
with sqlite3.connect(db) as con:
sql_cmd = '''
DROP TABLE IF EXISTS person;
CREATE TABLE person(
obs INTEGER PRIMARY KEY AUTOINCREMENT,
gender TEXT,
weight REAL,
height REAL);
'''
con.executescript(sql_cmd)
except Exception as e:
print('Error -> {}'.format(e))
def create_data(db, rows=10):
data = [(
random.choice('MF'),
random.normalvariate(57, 4.5),
random.normalvariate(160, 6),
) for _ in range(rows)]
print(data)
try:
with sqlite3.connect(db) as con:
sql_cmd = 'INSERT INTO person(gender, weight, height) VALUES (?, ?, ?);'
con.executemany(sql_cmd, data)
except Exception as e:
print('Error -> {}'.format(e))
def pd_read_sql(db):
with sqlite3.connect(db) as con:
sql_cmd = '''
SELECT obs, gender, weight, height
FROM person
ORDER BY height'''
df = pd.read_sql(sql_cmd,
con, index_col='obs')
print(df)
df['bmi'] = df.weight / (df.height / 100) ** 2
# df['height'].plot.hist(alpha=.7)
# df.plot.bar(x=df.index, y='weight')
df.plot.scatter(x='weight', y='height')
print(f'mean(bmi) = {df.bmi.mean():.2f}') # f-string in Python 3.6 (php: variable substitution, c#: string interpolation
plt.show()
if __name__ == '__main__':
db = 'sample.sqlite'
create_table(db)
create_data(db, 100)
pd_read_sql(db)
Text
Read text from URL
import urllib.request
# https://dl.dropboxusercontent.com/u/23148470/planet.txt
def read_from_url(link):
with urllib.request.urlopen(link) as f:
s = f.read()
# print(s)
# print(type(s))
# s1 = s.decode("utf8")
# print(type(s1))
# print(s1)
return s.decode("utf8")
def read_from_url2(link):
with urllib.request.urlopen(link) as f:
for line in f:
# print("- {}".format(line))
print("- {}".format(line.decode("utf8")), end="")
# print(s)
# print(type(s))
# s1 = s.decode("utf8")
# print(type(s1))
# print(s1)
t = read_from_url("https://dl.dropboxusercontent.com/u/23148470/planet.txt")
print("t = ", t)
lines = t.splitlines()
print("lines = ", lines)
# read_from_url2("https://dl.dropboxusercontent.com/u/23148470/planet.txt")
Last updated