第一章 Pandas基础
import pandas as pd
查看Pandas版本
pd.__version__
'1.0.3'
import numpy as np
一、文件读取与写入
1.读取
(a) csv格式
df = pd.read_csv('/Users/dingyunfei/Downloads/data/table.csv')
df.head()
|
School |
Class |
ID |
Gender |
Address |
Height |
Weight |
Math |
Physics |
0 |
S_1 |
C_1 |
1101 |
M |
street_1 |
173 |
63 |
34.0 |
A+ |
1 |
S_1 |
C_1 |
1102 |
F |
street_2 |
192 |
73 |
32.5 |
B+ |
2 |
S_1 |
C_1 |
1103 |
M |
street_2 |
186 |
82 |
87.2 |
B+ |
3 |
S_1 |
C_1 |
1104 |
F |
street_2 |
167 |
81 |
80.4 |
B- |
4 |
S_1 |
C_1 |
1105 |
F |
street_4 |
159 |
64 |
84.8 |
B+ |
(b) txt格式
df_txt = pd.read_table('/Users/dingyunfei/Downloads/data/table.txt')
df_txt
|
col1 |
col2 |
col3 |
col4 |
0 |
2 |
a |
1.4 |
apple |
1 |
3 |
b |
3.4 |
banana |
2 |
6 |
c |
2.5 |
orange |
3 |
5 |
d |
3.2 |
lemon |
© xls或xlsx格式
df_excel = pd.read_excel('/Users/dingyunfei/Downloads/data/table.xlsx')
df_excel.head()
|
School |
Class |
ID |
Gender |
Address |
Height |
Weight |
Math |
Physics |
0 |
S_1 |
C_1 |
1101 |
M |
street_1 |
173 |
63 |
34.0 |
A+ |
1 |
S_1 |
C_1 |
1102 |
F |
street_2 |
192 |
73 |
32.5 |
B+ |
2 |
S_1 |
C_1 |
1103 |
M |
street_2 |
186 |
82 |
87.2 |
B+ |
3 |
S_1 |
C_1 |
1104 |
F |
street_2 |
167 |
81 |
80.4 |
B- |
4 |
S_1 |
C_1 |
1105 |
F |
street_4 |
159 |
64 |
84.8 |
B+ |
2.写入
(a) csv格式
df.to_csv('/Users/dingyunfei/Downloads/data/new_table.csv')
(b) xls或xlsx格式
df.to_excel('/Users/dingyunfei/Downloads/data/new_table2.xlsx', sheet_name='Sheet1')
二、 基本数据结构
1.Series
(a) 创建一个Series
对于一个Series,其中最常用的属性为值(values),索引(index),名字(name),类型(dtype)
s = pd.Series(np.random.randn(5),index=['a','b','c','d','e'],name='这是一个Series',dtype='float64')
s
a -0.076623
b -0.962636
c 1.677339
d 0.601838
e 2.566811
Name: 这是一个Series, dtype: float64
(b) 访问Series属性
s.values
array([-0.07662251, -0.96263594, 1.6773387 , 0.60183849, 2.56681093])
s.name
'这是一个Series'
s.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
s.dtype
dtype('float64')
© 取出某一个元素
s['a']
-0.07662250989973526
(d) 调用方法
s.mean()
0.7613459332470334
print([attr for attr in dir(s) if not attr.startswith('_')])
['T', 'a', 'abs', 'add', 'add_prefix', 'add_suffix', 'agg', 'aggregate', 'align', 'all', 'any', 'append', 'apply', 'argmax', 'argmin', 'argsort', 'array', 'asfreq', 'asof', 'astype', 'at', 'at_time', 'attrs', 'autocorr', 'axes', 'b', 'between', 'between_time', 'bfill', 'bool', 'c', 'clip', 'combine', 'combine_first', 'convert_dtypes', 'copy', 'corr', 'count', 'cov', 'cummax', 'cummin', 'cumprod', 'cumsum', 'd', 'describe', 'diff', 'div', 'divide', 'divmod', 'dot', 'drop', 'drop_duplicates', 'droplevel', 'dropna', 'dtype', 'dtypes', 'duplicated', 'e', 'empty', 'eq', 'equals', 'ewm', 'expanding', 'explode', 'factorize', 'ffill', 'fillna', 'filter', 'first', 'first_valid_index', 'floordiv', 'ge', 'get', 'groupby', 'gt', 'hasnans', 'head', 'hist', 'iat', 'idxmax', 'idxmin', 'iloc', 'index', 'infer_objects', 'interpolate', 'is_monotonic', 'is_monotonic_decreasing', 'is_monotonic_increasing', 'is_unique', 'isin', 'isna', 'isnull', 'item', 'items', 'iteritems', 'keys', 'kurt', 'kurtosis', 'last', '