Data Loading, Storage,
數據加載, 存儲與文件格式
xiaoyao
import numpy as np
import pandas as pd
np.random.seed(12345)
import matplotlib.pyplot as plt
plt.rc('figure', figsize=(10, 6))
np.set_printoptions(precision=4, suppress=True)
Reading and Writing Data in Text Format
讀取,將數據寫入到text文件中
# read_csv默認以','間隔
df = pd.read_csv('examples/ex1.csv')
df
a | b | c | d | message | |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | hello |
1 | 5 | 6 | 7 | 8 | world |
2 | 9 | 10 | 11 | 12 | foo |
# read_table默認以'\t'分隔
pd.read_table('examples/ex1.csv', sep=',')
a | b | c | d | message | |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | hello |
1 | 5 | 6 | 7 | 8 | world |
2 | 9 | 10 | 11 | 12 | foo |
"""有的文件沒有標題行,可以通過pandas爲其分配默認的列名,也可以自定義列名
"""
pd.read_csv('examples/ex2.csv', names=['a', 'b', 'c', 'd', 'message'])
a | b | c | d | message | |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | hello |
1 | 5 | 6 | 7 | 8 | world |
2 | 9 | 10 | 11 | 12 | foo |
pd.read_csv('examples/ex2.csv', header=None)
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | hello |
1 | 5 | 6 | 7 | 8 | world |
2 | 9 | 10 | 11 | 12 | foo |
"""
如果希望將message列做成DataFrame的索引,可以明確表示將該列放到索引4的位置上,也可以通過
index_col參數指定“message”
"""
names = ['a', 'b', 'c', 'd', 'message']
pd.read_csv('examples/ex2.csv', names=names, index_col='message')
a | b | c | d | |
---|---|---|---|---|
message | ||||
hello | 1 | 2 | 3 | 4 |
world | 5 | 6 | 7 | 8 |
foo | 9 | 10 | 11 | 12 |
# !cat examples/csv_mindex.csv
parsed = pd.read_csv('examples/csv_mindex.csv',
index_col=['key1', 'key2'])
parsed
value1 | value2 | ||
---|---|---|---|
key1 | key2 | ||
one | a | 1 | 2 |
b | 3 | 4 | |
c | 5 | 6 | |
d | 7 | 8 | |
two | a | 9 | 10 |
b | 11 | 12 | |
c | 13 | 14 | |
d | 15 | 16 |
list(open('examples/ex3.txt'))
[' A B C\n',
'aaa -0.264438 -1.026059 -0.619500\n',
'bbb 0.927272 0.302904 -0.032399\n',
'ccc -0.264273 -0.386314 -0.217601\n',
'ddd -0.871858 -0.348382 1.100491\n']
有些表格可能不是用固定的分隔符去分隔字段的(比如說:空白字符或者其他模式)。有些表格可能不是用固定的分隔字段的(比如空白字符或者其他模式來分隔字段)。
正則表達式中\s匹配任何空白字符,包括空格、製表符、換頁符等等, 等價於[ \f\n\r\t\v]
- \f -> 匹配一個換頁
- \n -> 匹配一個換行符
- \r -> 匹配一個回車符
- \t -> 匹配一個製表符
- \v -> 匹配一個垂直製表符
而“\s+”則表示匹配任意多個上面的字符。
"""
雖然可以手動對數據進行規整,這裏的字段是被不同空白字符間隔開的。這種情況下,可以傳遞一個
正則表達式作爲read_table的分隔符。可以使用正則表達式表達爲: \s+
"""
result = pd.read_table('examples/ex3.txt', sep='\s+')
result
A | B | C | |
---|---|---|---|
aaa | -0.264438 | -1.026059 | -0.619500 |
bbb | 0.927272 | 0.302904 | -0.032399 |
ccc | -0.264273 | -0.386314 | -0.217601 |
ddd | -0.871858 | -0.348382 | 1.100491 |
# 使用skiprows跳過文件的第一行,第三行,第四行
pd.read_csv('examples/ex4.csv', skiprows=[0, 2, 3])
a | b | c | d | message | |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | hello |
1 | 5 | 6 | 7 | 8 | world |
2 | 9 | 10 | 11 | 12 | foo |
# 缺失值處理是文件解析任務中的一個重要的組成部分。
# 缺失數據經常是沒有(空字符串)要麼使用某個標記值進行表示。
# 默認情況之下,pandas或使用一組經常出現的標記值進行識別,比如:NA以及NULL
result = pd.read_csv('examples/ex5.csv')
result
something | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | one | 1 | 2 | 3.0 | 4 | NaN |
1 | two | 5 | 6 | NaN | 8 | world |
2 | three | 9 | 10 | 11.0 | 12 | foo |
pd.isnull(result)
something | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | False | False | False | False | False | True |
1 | False | False | False | True | False | False |
2 | False | False | False | False | False | False |
# na_values可以使用一個列表或者集合的字符串表示缺失值
result = pd.read_csv('examples/ex5.csv', na_values=['NULL'])
result
something | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | one | 1 | 2 | 3.0 | 4 | NaN |
1 | two | 5 | 6 | NaN | 8 | world |
2 | three | 9 | 10 | 11.0 | 12 | foo |
# 字典的各列可以使用不同的NA標記值
sentinels = {'message': ['foo', 'NA'], 'something': ['two']}
pd.read_csv('examples/ex5.csv', na_values=sentinels)
something | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | one | 1 | 2 | 3.0 | 4 | NaN |
1 | NaN | 5 | 6 | NaN | 8 | world |
2 | three | 9 | 10 | 11.0 | 12 | NaN |
這裏列舉出pandas.read_csv和pandas.read_tabel常用的選項。
參數 | 說明 |
---|---|
path | 表示文件系統的位置,url,文件型對象的字符串 |
sep或者delimiter | 用於對行中各字段進行拆分的字符序列或者正則表達式 |
header | 用作列名的行號。默認爲0,表示第一行,如果沒有header行就應該進行設置爲None |
index_col | 用作行索引的列表好或者列名,可以是單個名稱或者/數字組成的列表(層次化索引) |
names | 用於結果的列名列表,結合header=None |
Reading Text Files in Pieces 逐塊讀取文本文件
在處理很大的文件的時候,或者找出大文件中的參數集以便於後續處理,可只想讀取文件的一小部分或逐塊對文件進行迭代。
# 在看大文件之前,可以先設置pandas顯示更加緊密一些
pd.options.display.max_rows = 10
result = pd.read_csv('examples/ex6.csv')
result
one | two | three | four | key | |
---|---|---|---|---|---|
0 | 0.467976 | -0.038649 | -0.295344 | -1.824726 | L |
1 | -0.358893 | 1.404453 | 0.704965 | -0.200638 | B |
2 | -0.501840 | 0.659254 | -0.421691 | -0.057688 | G |
3 | 0.204886 | 1.074134 | 1.388361 | -0.982404 | R |
4 | 0.354628 | -0.133116 | 0.283763 | -0.837063 | Q |
... | ... | ... | ... | ... | ... |
9995 | 2.311896 | -0.417070 | -1.409599 | -0.515821 | L |
9996 | -0.479893 | -0.650419 | 0.745152 | -0.646038 | E |
9997 | 0.523331 | 0.787112 | 0.486066 | 1.093156 | K |
9998 | -0.362559 | 0.598894 | -1.843201 | 0.887292 | G |
9999 | -0.096376 | -1.012999 | -0.657431 | -0.573315 | 0 |
10000 rows × 5 columns
# 如果只想讀入幾行(避免讀取整個文件),可以通過nrows進行指定即可。
pd.read_csv('examples/ex6.csv', nrows=5)
one | two | three | four | key | |
---|---|---|---|---|---|
0 | 0.467976 | -0.038649 | -0.295344 | -1.824726 | L |
1 | -0.358893 | 1.404453 | 0.704965 | -0.200638 | B |
2 | -0.501840 | 0.659254 | -0.421691 | -0.057688 | G |
3 | 0.204886 | 1.074134 | 1.388361 | -0.982404 | R |
4 | 0.354628 | -0.133116 | 0.283763 | -0.837063 | Q |
# 如果需要逐塊讀取文件,可以指定chunksize(行數)
chunker = pd.read_csv('examples/ex6.csv', chunksize=1000)
chunker
<pandas.io.parsers.TextFileReader at 0x23892e3d848>
import warnings
warnings.filterwarnings('ignore')
迭代處理ex6.csv,將值計數聚合到“key”列中,操作如下;
chunker = pd.read_csv('examples/ex6.csv', chunksize=1000)
tot = pd.Series([])
for piece in chunker:
tot = tot.add(piece['key'].value_counts(), fill_value=0)
tot = tot.sort_values(ascending=False)
tot[:10]
E 368.0
X 364.0
L 346.0
O 343.0
Q 340.0
M 338.0
J 337.0
F 335.0
K 334.0
H 330.0
dtype: float64
textParser還有一個get_chunk方法,它可以讀取任意大小的塊。
Writing Data to Text Format 將數據寫出到文本格式
data = pd.read_csv('examples/ex5.csv')
data
something | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | one | 1 | 2 | 3.0 | 4 | NaN |
1 | two | 5 | 6 | NaN | 8 | world |
2 | three | 9 | 10 | 11.0 | 12 | foo |
# 數據可以被輸出爲分隔符格式的文本
# 利用DataFrame中的to_csv方法,可以將數據寫入到一個以逗號分隔的文件中
data.to_csv('examples/out.csv')
# !cat examples/out.csv
import sys
# 也可以使用其他的分隔符,這裏直接寫入到sys.stdout,所以僅僅是打印出文本結果而已
data.to_csv(sys.stdout, sep='|')
|something|a|b|c|d|message
0|one|1|2|3.0|4|
1|two|5|6||8|world
2|three|9|10|11.0|12|foo
# 缺失值在輸出結果中被表示爲空字符串。這裏我希望將其表示爲別的標記值:
data.to_csv(sys.stdout, na_rep='NULL')
,something,a,b,c,d,message
0,one,1,2,3.0,4,NULL
1,two,5,6,NULL,8,world
2,three,9,10,11.0,12,foo
# 如果沒有設置其他的選項,則會寫出行和列的標籤。也可以被禁用
data.to_csv(sys.stdout, index=False, header=False)
one,1,2,3.0,4,
two,5,6,,8,world
three,9,10,11.0,12,foo
# 也可以只寫出一部分的列,同時以我指定的順序排序
data.to_csv(sys.stdout, index=False, columns=['a', 'b', 'c'])
a,b,c
1,2,3.0
5,6,
9,10,11.0
Series也有一個to_csv方法
dates = pd.date_range('1/1/2000', periods=7)
ts = pd.Series(np.arange(7), index=dates)
ts.to_csv('examples/tseries.csv')
# !cat examples/tseries.csv
datesfile = pd.read_csv('examples/tseries.csv')
datesfile
Unnamed: 0 | 0 | |
---|---|---|
0 | 2000-01-01 | 0 |
1 | 2000-01-02 | 1 |
2 | 2000-01-03 | 2 |
3 | 2000-01-04 | 3 |
4 | 2000-01-05 | 4 |
5 | 2000-01-06 | 5 |
6 | 2000-01-07 | 6 |
Working with Delimited Formats 處理分隔符格式
大部分存儲在磁盤上的表格型數據都可以使用pandas.read_table進行加載,但有時需要做一些手工處理。實際上,因爲接收到含有畸形行的文件而使得read_table出毛病的情況很常見。
# 將任意打開的文件或者文件型的對象傳給csv.reader:對這個reader進行迭代將會產生一個元組(
# 同時:移除了所有的引號)
import csv
f = open('examples/ex7.csv')
reader = csv.reader(f)
for line in reader:
print(line)
f.close()
爲了使得數據格式合乎要求,進行處理。
首先,讀取文件到一個多行的列表中;
with open('examples/ex7.csv') as f:
lines = list(csv.reader(f))
# 將這些行分開爲;標題行和數據行
header, values = lines[0], lines[1:]
# 關於zip 函數的使用
a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped = zip(a,b) # 打包爲元組的列表
print(zipped)
<zip object at 0x00000238958F5808>
print(zip(a,c) ) # 元素個數與最短的列表一致
print((*zipped) )
<zip object at 0x0000023895828988>
# 然後,使用字典構造式和zip(*values),後者將行轉置爲列,創建數據列的字典:
"""
zip() 函數用於將可迭代的對象作爲參數,將對象中對應的元素打包成一個個元組,然後返回由這些元組組成的列表。
如果各個迭代器的元素個數不一致,則返回列表長度與最短的對象相同,利用 * 號操作符,可以將元組解壓爲列表。
"""
data_dict = {h: v for h, v in zip(header, zip(*values))}
data_dict
{'a': ('1', '1'), 'b': ('2', '2'), 'c': ('3', '3')}
class my_dialect(csv.Dialect):
lineterminator = ‘\n’
delimiter = ‘;’
quotechar = ‘"’
quoting = csv.QUOTE_MINIMAL
reader = csv.reader(f, dialect=my_dialect)
reader = csv.reader(f, delimiter=’|’)
with open(‘mydata.csv’, ‘w’) as f:
writer = csv.writer(f, dialect=my_dialect)
writer.writerow((‘one’, ‘two’, ‘three’))
writer.writerow((‘1’, ‘2’, ‘3’))
writer.writerow((‘4’, ‘5’, ‘6’))
writer.writerow((‘7’, ‘8’, ‘9’))
JSON Data JSON數據
JSON已經成爲:通過http請求在web瀏覽器和其他的應用程序之間發送數據的標準格式之一。他比表格型的數據更加靈活。
obj = """
{"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38,
"pets": ["Sixes", "Stache", "Cisco"]}]
}
"""
import json
result = json.loads(obj)
result
{'name': 'Wes',
'places_lived': ['United States', 'Spain', 'Germany'],
'pet': None,
'siblings': [{'name': 'Scott', 'age': 30, 'pets': ['Zeus', 'Zuko']},
{'name': 'Katie', 'age': 38, 'pets': ['Sixes', 'Stache', 'Cisco']}]}
# json.dumps則將python對象轉換爲JSON格式。
asjson = json.dumps(result)
將(一個或者一組)JSON對象轉換爲DataFrame或者其他便於分析的數據結構?
最簡單的方式就是:往DataFrame構造器傳入一個字典的列表(就是原先的JSON對象),並選取數據字段的子集.
siblings = pd.DataFrame(result['siblings'], columns=['name', 'age'])
siblings
name | age | |
---|---|---|
0 | Scott | 30 |
1 | Katie | 38 |
# pandas.read_json可以自動將特別格式的json數據集轉換爲Series或者DataFrame.
# pandas.read_json的默認選項假設json數組中的每個對象是表格中的一行
data = pd.read_json('examples/example.json')
data
a | b | c | |
---|---|---|---|
0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 |
2 | 7 | 8 | 9 |
# 如果需要將數據從pandas輸出到json,可以使用to_json方法;
print(data.to_json())
print(data.to_json(orient='records'))
{"a":{"0":1,"1":4,"2":7},"b":{"0":2,"1":5,"2":8},"c":{"0":3,"1":6,"2":9}}
[{"a":1,"b":2,"c":3},{"a":4,"b":5,"c":6},{"a":7,"b":8,"c":9}]
XML and HTML: Web Scraping Web信息收集
python有許多的可以讀寫常見的html和xml格式數據的庫,包括:lxml,beautiful soup和 html5lib.
lxml的速度比較快,但是,,,,其他的庫處理有誤的html或者xml文件更好。
pandas有一個內置的功能,read_html,它可以使用lxml和beautiful soup 自動將html文件中的表格解析爲DataFrame對象。
- conda install lxml # 或者 pip install lxml
- pip install beautifulsoup4 html5lib
# 這裏的html文件記錄了銀行倒閉的情況
tables = pd.read_html('examples/fdic_failed_bank_list.html')
len(tables)
1
failures = tables[0]
failures.head()
Bank Name | City | ST | CERT | Acquiring Institution | Closing Date | Updated Date | |
---|---|---|---|---|---|---|---|
0 | Allied Bank | Mulberry | AR | 91 | Today's Bank | September 23, 2016 | November 17, 2016 |
1 | The Woodbury Banking Company | Woodbury | GA | 11297 | United Bank | August 19, 2016 | November 17, 2016 |
2 | First CornerStone Bank | King of Prussia | PA | 35312 | First-Citizens Bank & Trust Company | May 6, 2016 | September 6, 2016 |
3 | Trust Company Bank | Memphis | TN | 9956 | The Bank of Fayette County | April 29, 2016 | September 6, 2016 |
4 | North Milwaukee State Bank | Milwaukee | WI | 20364 | First-Citizens Bank & Trust Company | March 11, 2016 | June 16, 2016 |
因爲failures有許多列,pandas插入了一個換行符\
# 計算按照年份倒閉的銀行數量
close_timestamps = pd.to_datetime(failures['Closing Date'])
close_timestamps.dt.year.value_counts()
2010 157
2009 140
2011 92
2012 51
2008 25
...
2004 4
2001 4
2007 3
2003 3
2000 2
Name: Closing Date, Length: 15, dtype: int64
Parsing XML with lxml.objectify 利用lxml.objectify解析xml
"""
<INDICATOR>
<INDICATOR_SEQ>373889</INDICATOR_SEQ>
<PARENT_SEQ></PARENT_SEQ>
<AGENCY_NAME>Metro-North Railroad</AGENCY_NAME>
<INDICATOR_NAME>Escalator Availability</INDICATOR_NAME>
<DESCRIPTION>Percent of the time that escalators are operational
systemwide. The availability rate is based on physical observations performed
the morning of regular business days only. This is a new indicator the agency
began reporting in 2009.</DESCRIPTION>
<PERIOD_YEAR>2011</PERIOD_YEAR>
<PERIOD_MONTH>12</PERIOD_MONTH>
<CATEGORY>Service Indicators</CATEGORY>
<FREQUENCY>M</FREQUENCY>
<DESIRED_CHANGE>U</DESIRED_CHANGE>
<INDICATOR_UNIT>%</INDICATOR_UNIT>
<DECIMAL_PLACES>1</DECIMAL_PLACES>
<YTD_TARGET>97.00</YTD_TARGET>
<YTD_ACTUAL></YTD_ACTUAL>
<MONTHLY_TARGET>97.00</MONTHLY_TARGET>
<MONTHLY_ACTUAL></MONTHLY_ACTUAL>
</INDICATOR>
"""
%pwd
'D:\\python code\\8messy\\4利用python進行數據分析\\pydata-book-2nd-edition'
# 先使用lxml.objectify解析該文件,然後通過getroot得到該xml文件的根節點的引用:
from lxml import objectify
path = 'datasets/mta_perf/Performance_MNR.xml'
parsed = objectify.parse(open(path))
root = parsed.getroot()
root.INDICATOR返回一個用於產生xml元素的生成器。對於每條記錄,使用標記名和數據值填充一個字典。
data = []
skip_fields = ['PARENT_SEQ', 'INDICATOR_SEQ',
'DESIRED_CHANGE', 'DECIMAL_PLACES']
for elt in root.INDICATOR:
el_data = {}
for child in elt.getchildren():
if child.tag in skip_fields:
continue
el_data[child.tag] = child.pyval
data.append(el_data)
perf = pd.DataFrame(data)
perf.head()
AGENCY_NAME | INDICATOR_NAME | DESCRIPTION | PERIOD_YEAR | PERIOD_MONTH | CATEGORY | FREQUENCY | INDICATOR_UNIT | YTD_TARGET | YTD_ACTUAL | MONTHLY_TARGET | MONTHLY_ACTUAL | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Metro-North Railroad | On-Time Performance (West of Hudson) | Percent of commuter trains that arrive at thei... | 2008 | 1 | Service Indicators | M | % | 95 | 96.9 | 95 | 96.9 |
1 | Metro-North Railroad | On-Time Performance (West of Hudson) | Percent of commuter trains that arrive at thei... | 2008 | 2 | Service Indicators | M | % | 95 | 96 | 95 | 95 |
2 | Metro-North Railroad | On-Time Performance (West of Hudson) | Percent of commuter trains that arrive at thei... | 2008 | 3 | Service Indicators | M | % | 95 | 96.3 | 95 | 96.9 |
3 | Metro-North Railroad | On-Time Performance (West of Hudson) | Percent of commuter trains that arrive at thei... | 2008 | 4 | Service Indicators | M | % | 95 | 96.8 | 95 | 98.3 |
4 | Metro-North Railroad | On-Time Performance (West of Hudson) | Percent of commuter trains that arrive at thei... | 2008 | 5 | Service Indicators | M | % | 95 | 96.6 | 95 | 95.8 |
from io import StringIO
tag = '<a href="http://www.google.com">Google</a>'
root = objectify.parse(StringIO(tag)).getroot()
root
root.get('href')
root.text
'Google'
Binary Data Formats 二進制數據格式
實現數據的高效二進制格式存儲的最有效的辦法之一是使用python內置的pickle序列化。
pandas對象都有一個用於將數據以pickle格式保存到磁盤上的to_pickle方法:
frame = pd.read_csv('examples/ex1.csv')
frame
frame.to_pickle('examples/frame_pickle')
# 可以通過pickle 直接讀取被pickle化的數據,或者是使用更爲方便的pandas.read_pickle
pd.read_pickle('examples/frame_pickle')
a | b | c | d | message | |
---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | hello |
1 | 5 | 6 | 7 | 8 | world |
2 | 9 | 10 | 11 | 12 | foo |
注意;pickle僅僅建議用於短期的存儲格式。原因是很難保證該格式永遠是穩定的;現在的pickle對象可能無法被後續版本的庫unpickle出來。
Using HDF5 Format 使用hdf5格式
HDF5和MessagePack,,pandas內置支持的兩個二進制數據格式。
frame = pd.DataFrame({'a': np.random.randn(100)})
store = pd.HDFStore('mydata.h5')
store['obj1'] = frame
store['obj1_col'] = frame['a']
store
<class 'pandas.io.pytables.HDFStore'>
File path: mydata.h5
# HDF5文件中的對象可以通過與字典一樣的api進行獲取
store['obj1']
a | |
---|---|
0 | -0.204708 |
1 | 0.478943 |
2 | -0.519439 |
3 | -0.555730 |
4 | 1.965781 |
... | ... |
95 | 0.795253 |
96 | 0.118110 |
97 | -0.748532 |
98 | 0.584970 |
99 | 0.152677 |
100 rows × 1 columns
# HDFStore支持兩種存儲模式,fixed和table,後者會更慢,但是
# 支持使用特殊語法進行查詢操作
store.put('obj2', frame, format='table')
store.select('obj2', where=['index >= 10 and index <= 15'])
store.close()
frame.to_hdf('mydata.h5', 'obj3', format='table')
pd.read_hdf('mydata.h5', 'obj3', where=['index < 5'])
a | |
---|---|
0 | -0.204708 |
1 | 0.478943 |
2 | -0.519439 |
3 | -0.555730 |
4 | 1.965781 |
import os
os.remove('mydata.h5')
HDF5不是數據庫,它最適合用作’"一次寫多次讀"的數據集。雖然數據可以在任何時候被添加到文件中,但是如果同時發送多個寫操作,文件就可能被破壞。
Reading Microsoft Excel Files 讀取Microsoft excel文件
xlsx = pd.ExcelFile('examples/ex1.xlsx')
pd.read_excel(xlsx, 'Sheet1')
Unnamed: 0 | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | 0 | 1 | 2 | 3 | 4 | hello |
1 | 1 | 5 | 6 | 7 | 8 | world |
2 | 2 | 9 | 10 | 11 | 12 | foo |
frame = pd.read_excel('examples/ex1.xlsx', 'Sheet1')
frame
Unnamed: 0 | a | b | c | d | message | |
---|---|---|---|---|---|---|
0 | 0 | 1 | 2 | 3 | 4 | hello |
1 | 1 | 5 | 6 | 7 | 8 | world |
2 | 2 | 9 | 10 | 11 | 12 | foo |
"""
如果要將pandas數據寫入爲excel格式,必須像創建一個excelWriter,然後使用pandas對象的to_excel
方法將數據寫入其中;
"""
writer = pd.ExcelWriter('examples/ex2.xlsx')
frame.to_excel(writer, 'Sheet1')
writer.save()
# 也可以不用ExcelWriter,而是傳遞文件的路徑到to_excel;
frame.to_excel('examples/ex2.xlsx')
Interacting with Web APIs Web APIs交互
import requests
url = 'https://api.github.com/repos/pandas-dev/pandas/issues'
resp = requests.get(url)
resp
<Response [200]>
data = resp.json()
data[0]['title']
'BUG: resample().nearest() throws an exception if tz is provided for the DatetimeIndex'
issues = pd.DataFrame(data, columns=['number', 'title',
'labels', 'state'])
issues
number | title | labels | state | |
---|---|---|---|---|
0 | 33895 | BUG: resample().nearest() throws an exception ... | [{'id': 76811, 'node_id': 'MDU6TGFiZWw3NjgxMQ=... | open |
1 | 33894 | ENH: Construct pandas dataframe from function | [{'id': 76812, 'node_id': 'MDU6TGFiZWw3NjgxMg=... | open |
2 | 33892 | COMPAT: add back block manager constructors to... | [{'id': 76865106, 'node_id': 'MDU6TGFiZWw3Njg2... | open |
3 | 33891 | pandas interpolate inconsistent results with a... | [] | open |
4 | 33890 | CI: failing timedelta64 test on Linux py37_loc... | [{'id': 48070600, 'node_id': 'MDU6TGFiZWw0ODA3... | open |
... | ... | ... | ... | ... |
25 | 33851 | DOC: Single Document For Code Guidelines | [{'id': 134699, 'node_id': 'MDU6TGFiZWwxMzQ2OT... | open |
26 | 33849 | BUG: weird behaviour of pivot_table when aggf... | [{'id': 76811, 'node_id': 'MDU6TGFiZWw3NjgxMQ=... | open |
27 | 33846 | BUG: Series construction with EA dtype and ind... | [{'id': 76811, 'node_id': 'MDU6TGFiZWw3NjgxMQ=... | open |
28 | 33845 | TestDatetimeIndex.test_reindex_with_same_tz 32... | [{'id': 563047854, 'node_id': 'MDU6TGFiZWw1NjM... | open |
29 | 33843 | Roll quantile support multiple quantiles as pe... | [] | open |
30 rows × 4 columns
Interacting with Databases 數據庫交互
import sqlite3
query = """
CREATE TABLE test
(a VARCHAR(20), b VARCHAR(20),
c REAL, d INTEGER
);"""
con = sqlite3.connect('mydata.sqlite')
con.execute(query)
con.commit()
data = [('Atlanta', 'Georgia', 1.25, 6),
('Tallahassee', 'Florida', 2.6, 3),
('Sacramento', 'California', 1.7, 5)]
stmt = "INSERT INTO test VALUES(?, ?, ?, ?)"
con.executemany(stmt, data)
con.commit()
cursor = con.execute('select * from test')
rows = cursor.fetchall()
rows
[('Atlanta', 'Georgia', 1.25, 6),
('Tallahassee', 'Florida', 2.6, 3),
('Sacramento', 'California', 1.7, 5)]
cursor.description
pd.DataFrame(rows, columns=[x[0] for x in cursor.description])
import sqlalchemy as sqla
db = sqla.create_engine('sqlite:///mydata.sqlite')
pd.read_sql('select * from test', db)
!rm mydata.sqlite