数据的创建与基本操作
- 建一个dataframe 使用时间序列为行索引,使用abcdef为列索引
import numpy as np
import pandas as pd
dates = pd.date_range("20170813",periods=6)
df = pd.DataFrame(data=np.random.randint(3, 9,size=(6,6)),index=dates,columns=list(["a","b","c","d","e","f"]))
print(df)
a b c d e f
2017-08-13 5 3 7 8 8 6
2017-08-14 3 7 5 3 3 7
2017-08-15 5 3 8 8 8 5
2017-08-16 4 3 8 8 3 8
2017-08-17 6 5 5 4 4 8
2017-08-18 6 6 4 3 7 5
print(df.dtypes)
a int32
b int32
c int32
d int32
e int32
f int32
dtype: object
print(df.index)
DatetimeIndex(['2017-08-13', '2017-08-14', '2017-08-15', '2017-08-16',
'2017-08-17', '2017-08-18'],
dtype='datetime64[ns]', freq='D')
print(df.columns)
Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')
print(df.values)
array([[5, 3, 7, 8, 8, 6],
[3, 7, 5, 3, 3, 7],
[5, 3, 8, 8, 8, 5],
[4, 3, 8, 8, 3, 8],
[6, 5, 5, 4, 4, 8],
[6, 6, 4, 3, 7, 5]])
print(df.describe())
a b c d e f
count 6.000000 6.000000 6.000000 6.000000 6.000000 6.000000
mean 4.833333 4.500000 6.166667 5.666667 5.500000 6.500000
std 1.169045 1.760682 1.722401 2.581989 2.428992 1.378405
min 3.000000 3.000000 4.000000 3.000000 3.000000 5.000000
25% 4.250000 3.000000 5.000000 3.250000 3.250000 5.250000
50% 5.000000 4.000000 6.000000 6.000000 5.500000 6.500000
75% 5.750000 5.750000 7.750000 8.000000 7.750000 7.750000
max 6.000000 7.000000 8.000000 8.000000 8.000000 8.000000
print(df.sort_index(axis=1,ascending=False))
f e d c b a
2017-08-13 6 8 8 7 3 5
2017-08-14 7 3 3 5 7 3
2017-08-15 5 8 8 8 3 5
2017-08-16 8 3 8 8 3 4
2017-08-17 8 4 4 5 5 6
2017-08-18 5 7 3 4 6 6
print(df.sort_index(axis=0,ascending=False))
a b c d e f
2017-08-18 6 6 4 3 7 5
2017-08-17 6 5 5 4 4 8
2017-08-16 4 3 8 8 3 8
2017-08-15 5 3 8 8 8 5
2017-08-14 3 7 5 3 3 7
2017-08-13 5 3 7 8 8 6
- 对数据进行排序 指定在行上进行排序 并以倒序的形式
print(df.sort_values(by="2017-08-13",axis=1,ascending=False))
d e c f a b
2017-08-13 8 8 7 6 5 3
2017-08-14 3 3 5 7 3 7
2017-08-15 8 8 8 5 5 3
2017-08-16 8 3 8 8 4 3
2017-08-17 4 4 5 8 6 5
2017-08-18 3 7 4 5 6 6
- 对数据进行排序 指定在列上进行排序 并以倒序的形式
print(df.sort_values(by="e",axis=0,ascending=False))
a b c d e f
2017-08-13 5 3 7 8 8 6
2017-08-15 5 3 8 8 8 5
2017-08-18 6 6 4 3 7 5
2017-08-17 6 5 5 4 4 8
2017-08-14 3 7 5 3 3 7
2017-08-16 4 3 8 8 3 8