Skip to content

Commit a26df99

Browse files
committed
新增pandas测试
1 parent ccd31af commit a26df99

5 files changed

Lines changed: 1859 additions & 0 deletions

File tree

test/test_pandas.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
平台,分数,标记
2+
天猫,98,A
3+
京东,87,B
4+
苏宁,76,C
5+
国美,65,D
6+
淘宝,54,E

test/test_pandas.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
@author: zhanghe
6+
@software: PyCharm
7+
@file: test_pandas.py
8+
@time: 2017/2/27 下午3:21
9+
"""
10+
11+
12+
from pandas import Series, DataFrame
13+
import pandas as pd
14+
import json
15+
16+
17+
def test_csv():
18+
file_path = 'test_pandas.csv'
19+
df = pd.read_csv(file_path)
20+
print df.keys()
21+
print df.values
22+
print df.add_prefix('a')
23+
print df.add_suffix('c')
24+
print df.index
25+
print df.dtypes
26+
print df.rename(columns=['网站', '权重', '类型'])
27+
28+
29+
def test_excel():
30+
file_path = 'test_pandas.xls'
31+
df = pd.read_excel(file_path, sheetname='Sheet1') # sheet_name=str(0)
32+
print df
33+
34+
with pd.ExcelWriter('newxls.xls') as writer:
35+
df.to_excel(writer, sheet_name=str(0))
36+
37+
38+
def test_csv_encode():
39+
"""
40+
测试csv读取,设置编码,并跳过头部
41+
:return:
42+
"""
43+
file_path = 'test_pandas_gb2312.csv'
44+
df = pd.read_csv(file_path, encoding='gb2312', skiprows=8, error_bad_lines=False)
45+
print df.keys()
46+
print df
47+
48+
49+
def test_read_sem_excel():
50+
file_path = 'SEM.xls'
51+
df = pd.read_excel(file_path, sheetname='sem') # sheet_name=str(0)
52+
# [u'sem_site', u'sem_plan', u'sem_unit', u'sc_site', u'sc_site_name', u'cate_id', u'cate_name', u'city_id', u'city_name', u'sc_plat', u'sc_plat_name']
53+
print list(df.keys())
54+
# print df.to_dict()
55+
sem_map = {}
56+
for i in df.values:
57+
sem_map[(i[0], i[1], i[2])] = {
58+
'sc_site': i[3],
59+
'sc_site_name': i[4],
60+
'cate_id': i[5],
61+
'cate_name': i[6],
62+
'city_id': i[7],
63+
'city_name': i[8],
64+
'sc_plat': i[9],
65+
'sc_plat_name': i[10],
66+
}
67+
# print json.dumps(sem_map, indent=4, ensure_ascii=False)
68+
print sem_map
69+
return sem_map
70+
71+
72+
if __name__ == '__main__':
73+
# test_csv()
74+
# test_excel()
75+
# test_csv_encode()
76+
test_read_sem_excel()
77+
78+
79+
"""
80+
pip install pandas
81+
# 操作excel
82+
pip install xlrd
83+
"""
84+

test/test_pandas.xls

13.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)