import pandas as pd
import numpy as np
# Series
s1 = pd.Series([4, 7, -5, 3]) # 创建一个series,索引为默认值
print(s1)
print()
print(s1.values) # Series的值
print(s1.index) # Series的索引
print()
s2 = pd.Series([4.0, 6.5, -0.5, 4.2], index=['d', 'b', 'a', 'c'])
print(s2)
print()
print(s2['a']) # 根据索引取值
print()
print(s2[['a', 'b', 'c']]) # 根据索引取值
print()
print('b'in s2) # 判断在不在series中
print('e'in s2)
# series 可以看成是一个定长的有序的字典
dic1 = {'apple': 5, 'pen': 3, 'applepen': 10}
s3 = pd.Series(dic1)
print(s3)
# DataFrame
data = {'year': [2014, 2015, 2016, 2017],
'income': [10000, 30000, 50000, 80000],
'pay': [5000, 10000, 30000, 50000]
}
df1 = pd.DataFrame(data)
print(df1)
print()
df2 = pd.DataFrame(np.arange(12).reshape(3, 4))
print(df2)
print()
df3 = pd.DataFrame(np.arange(12).reshape(3, 4), index=['a', 'c', 'b'], columns=[1, 5, 66, 55])
print(df3)
print()
print(df1.columns) # 列的属性
print(df1.index) # 行的属性
print(df1.values) # 值
print(df1.describe()) #表的描述
print()
print(df1.T)
print()
print(df3.sort_index(axis=1)) # 对列进行排序
print()
print(df3.sort_index(axis=0)) # 对行进行排序
print()
print(df3.sort_values(by=66)) # 对66这一列的值进行排序
0 4
1 7
2 -5
3 3
dtype: int64
[ 4 7 -5 3]
RangeIndex(start=0, stop=4, step=1)
d 4.0
b 6.5
a -0.5
c 4.2
dtype: float64
-0.5
a -0.5
b 6.5
c 4.2
dtype: float64
True
False
apple 5
pen 3
applepen 10
dtype: int64
year income pay
0 2014 10000 5000
1 2015 30000 10000
2 2016 50000 30000
3 2017 80000 50000
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
1 5 66 55
a 0 1 2 3
c 4 5 6 7
b 8 9 10 11
Index(['year', 'income', 'pay'], dtype='object')
RangeIndex(start=0, stop=4, step=1)
[[ 2014 10000 5000]
[ 2015 30000 10000]
[ 2016 50000 30000]
[ 2017 80000 50000]]
year income pay
count 4.000000 4.000000 4.000000
mean 2015.500000 42500.000000 23750.000000
std 1.290994 29860.788112 20564.937799
min 2014.000000 10000.000000 5000.000000
25% 2014.750000 25000.000000 8750.000000
50% 2015.500000 40000.000000 20000.000000
75% 2016.250000 57500.000000 35000.000000
max 2017.000000 80000.000000 50000.000000
0 1 2 3
year 2014 2015 2016 2017
income 10000 30000 50000 80000
pay 5000 10000 30000 50000
1 5 55 66
a 0 1 3 2
c 4 5 7 6
b 8 9 11 10
1 5 66 55
a 0 1 2 3
b 8 9 10 11
c 4 5 6 7
1 5 66 55
a 0 1 2 3
c 4 5 6 7
b 8 9 10 11