一、Series
A Series is a single column of data, with row labels for each observation.
Pandas refers to the row labels as the index of the Series.
1. 默认下标
import numpy as np
import pandas as pd
S = pd.Series([11,28,72, 3, 5, 8])
print(S)
print(S.index) # 默认从0开始
print(S.values)
2. 自定义下标
fruits = ['apples','oranges','cherries','pears']
quantities = [20,33,52,19]
S = pd.Series(data = quantities,index = fruits)
print(S)
values = [5.6, 5.3, 4.3, 4.2, 5.8, 5.3, 4.6, 7.8, 9.1, 8., 5.7]
years = list(range(1995, 2017, 2))
unemp = pd.Series(data = values, index = years, name = 'Unemployment')
We can look at the index and values in our Series.
unemp.index
unemp.values
3. 对Seires数据进行简易处理
查看头尾数据
unemp.head()
unemp.tail()
基础绘图:使用.plot()方法
unemp.plot()
查看不重复的数据
unemp.unique()
4. Indexing
Sometimes, we will want to select particular elements from a Series.
We can do this using .loc[index_items]; where index_items is an item from the index, or a list of items in the index.
unemp.loc[1995]
unemp.loc[[1995,2005,2015]]
unemp.loc[1995:2007]
5. 对Series进行算数操作
print((S+3)*4)
print(np.sin(x))
S.apply(np.sin)
# 使用apply方法、叠加lambda函数
S.apply(lambda x : x if x>50 else x+10)
6. 序列拼接 pd.concat
参数axis = 0 表示在下方合并
x = pd.Series([1, 3, 5, 7])
y = pd.Series(['male','female','male','female'])
pd.concat([x,y], axis = 0)
axis = 1表示纵向合并
pd.concat([x,y], axis = 1)
7. 通过字典生成Series
cities = {'London':861546,'Berlin':173529,'Baecelona':184202}
city_series = pd.Series(cities)
print(city_series)
NaN: not a number
my_cities = ['London','Paris','Berlin','Hamburg']
my_city_series = pd.Series(cities,index=my_cities)
print(my_city_series)
8. isnull()、notnull()判断是否缺失
print(my_city_series.isnull())
print(my_city_series.notnull())
9. 过滤缺失元素
注意:dropna()方法不影响原数组,而是生成新数组
print(my_city_series.dropna()) # 默认axis = 0, 即删除行
print(my_city_series.dropna(inplace = True)) #原地操作
10. 填充缺失值
my_cities = ['London','Paris','Berlin','Hamburg']
my_city_series = pd.Series(cities,index=my_cities)
print(my_city_series.fillna(0)) # 用数值填充
11. 更改列名
names = {"NorthEast": "NE",
"MidWest": "MW",
"South": "S",
"West": "W"}
unemp_region.rename(columns=names)
12. pd.read_excel()/ read_csv() 参数
(1)sheet_name = ’ '指定读取sheet
(2)默认header = 0, 默认读取第一行为表头;header = None, 不使用数据源作为表头;
(3)index_col = None,默认索引为0的列作为行标签
(4)针对csv, parse_dates = [‘列名’] 自动将这些列转换为日期类型
13. pandas库好用的内置函数
(1)累计最大/最小/求和/乘积:cum(sum|min|max|prod)
(2)百分比变化:pct_change
(3)差分:diff()
(4)绝对值:abs
14. apply()方法
def standardize_data(x):
"""
Changes the data in a Series to become mean 0 with standard deviation 1
"""
mu = x.mean()
std = x.std()
return (x - mu)/std
std_unemp = unemp.apply(standardize_data)
std_unemp.head()
15. 花式索引
df.loc[(df["California"] < 5.1) & (df["Texas"].isin([4.6, 4.3]))]