您的位置:首页 > 其它

pandas入门——数据的创建与基本操作

2017-08-15 18:11 363 查看

数据的创建与基本操作

建一个dataframe 使用时间序列为行索引,使用abcdef为列索引

# 导入numpy包以np的形式;导入pandas包以pd的形式
import numpy as np
import pandas as pd

# 创建一个时间序列
dates = pd.date_range("20170813",periods=6)

# 创建一个dataframe 使用时间序列为行索引,使用abcdef为列索引
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


获取每一column的数据类型

print(df.dtypes)


a    int32
b    int32
c    int32
d    int32
e    int32
f    int32
dtype: object


获取数据的index

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')


获取数据的columns

print(df.columns)


Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')


获取数据的所有values

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pandas
相关文章推荐