1.Pandas Series与numpy array
不同点:
- Series是一个类数组的数据结构,同时带有标签(lable)或者说索引(index)。
- Series可使用s.describe()。
相同点:
- 通过索引获取元素。
- 遍历 for x in a:
- 可使用函数:mean(),max()。
2.pandas series运算
import pandas as pd
a = pd.Series([1, 2, 3, 4])
b = pd.Series([1, 2, 1, 2])
print a + b
print a * 2
print a >= 3
print a[a >= 3]
0 2
1 4
2 4
3 6
dtype: int64
0 2
1 4
2 6
3 8
dtype: int64
0 False
1 False
2 True
3 True
dtype: bool
2 3
3 4
dtype: int64
a.describe()
count 4.000000
mean 2.500000
std 1.290994
min 1.000000
25% 1.750000
50% 2.500000
75% 3.250000
max 4.000000
dtype: float64
向量化运算和Series索引
#索引相同
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
print s1 + s2
a 2
b 4
c 6
d 8
dtype: int64
#索引顺序不同
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([1, 2, 3, 4], index=['b', 'd', 'a', 'c'])
print s1 + s2
a 4
b 3
c 7
d 6
dtype: int64
#索引存在不同元素
s1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s2 = pd.Series([1, 2, 3, 4], index=['c', 'd', 'e', 'f'])
print s1 + s2
a NaN
b NaN
c 4.0
d 6.0
e NaN
f NaN
dtype: float64
# result.dropna() 去除NaN值