Pandas的基础操作(一)——矩阵表的创建及其属性

(注:记得在文件开头导入import numpy as np以及import pandas as pd)


 import pandas as pd
import numpy as np #创建一个Pandas序列 s = pd.Series([1, 3, 6, np.nan, 44, 1])
# print(s)
# 0 1.0
# 1 3.0
# 2 6.0
# 3 NaN
# 4 44.0
# 5 1.0
# dtype: float64 #创建一个矩阵式的DataFrame
dates = pd.date_range('', periods=6)
# print(dates)
# DatetimeIndex(['2019-07-10', '2019-07-11', '2019-07-12', '2019-07-13',
# '2019-07-14', '2019-07-15'],
# dtype='datetime64[ns]', freq='D') #行的标签是 dates ;列的标签是:columns
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['a', 'b', 'c', 'd'])
# print(df)
# a b c d
# 2019-07-10 -0.953463 -2.588401 1.680335 0.258889
# 2019-07-11 -2.183960 -1.559565 -0.119690 2.474845
# 2019-07-12 0.246754 0.237245 0.555891 -2.291064
# 2019-07-13 1.365473 -0.520804 2.351753 -0.650416
# 2019-07-14 0.160255 -0.665578 -1.330720 -0.502632
# 2019-07-15 1.427740 -0.386175 -0.102600 0.280338 #采用默认的行列标签方式,行和列的标签号都是从0开始
df1 = pd.DataFrame(np.arange(12).reshape((3,4)))
# print(df1)
# 0 1 2 3
# 0 0 1 2 3
# 1 4 5 6 7
# 2 8 9 10 11 #利用字典创建pandas矩阵表
df2 = pd.DataFrame({
'A':1.,
'B':pd.Timestamp(''),
'C':pd.Series(1, index=list(range(4)), dtype='float32'),
'D':np.array([3]*4, dtype='int32'),
'E':pd.Categorical(["test", "train", "test", "train"]),
'F':'foo'
}) # print(df2)
# A B C D E F
# 0 1.0 2019-07-10 1.0 3 test foo
# 1 1.0 2019-07-10 1.0 3 train foo
# 2 1.0 2019-07-10 1.0 3 test foo
# 3 1.0 2019-07-10 1.0 3 train foo
# #常用属性介绍(PS:因为是属性所以不用添加括号) #1 df.dtypes 查看每一列的数据类型
print(df2.dtypes)
# A float64
# B datetime64[ns]
# C float32
# D int32
# E category
# F object
# dtype: object #2 df.index 查看矩阵表所有每一行的标签号
print(df2.index)
# Int64Index([0, 1, 2, 3], dtype='int64') #3 df.columns 查看矩阵表所有每一列的标签号
print(df2.columns)
# Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object') #4 df.values 查看矩阵表中所有的值
print(df2.values)
# [[1.0 Timestamp('2019-07-10 00:00:00') 1.0 3 'test' 'foo']
# [1.0 Timestamp('2019-07-10 00:00:00') 1.0 3 'train' 'foo']
# [1.0 Timestamp('2019-07-10 00:00:00') 1.0 3 'test' 'foo']
# [1.0 Timestamp('2019-07-10 00:00:00') 1.0 3 'train' 'foo']] #5 df.describe() 描述矩阵表中float int 数据类型的均值、方差等参数
print(df2.describe())
# A C D
# count 4.0 4.0 4.0
# mean 1.0 1.0 3.0
# std 0.0 0.0 0.0
# min 1.0 1.0 3.0
# 25% 1.0 1.0 3.0
# 50% 1.0 1.0 3.0
# 75% 1.0 1.0 3.0
# max 1.0 1.0 3.0 #6 df.T 将矩阵表转置
print(df2.T)
# A 1 ... 1
# B 2019-07-10 00:00:00 ... 2019-07-10 00:00:00
# C 1 ... 1
# D 3 ... 3
# E test ... train
# F foo ... foo #7 df.sort_index() 对矩阵表进行排序 #按照索引标签进行排序,axis=1 --> 行位置不变,列位置变化,ascending=False,进行反排序
print(df2.sort_index(axis=1, ascending=False))
# F E D C B A
# 0 foo test 3 1.0 2019-07-10 1.0
# 1 foo train 3 1.0 2019-07-10 1.0
# 2 foo test 3 1.0 2019-07-10 1.0
# 3 foo train 3 1.0 2019-07-10 1.0 #按照索引标签进行排序,axis=0 --> 行位置变化,列位置不变,ascending=False,进行反排序
print(df2.sort_index(axis=0, ascending=False))
# A B C D E F
# 3 1.0 2019-07-10 1.0 3 train foo
# 2 1.0 2019-07-10 1.0 3 test foo
# 1 1.0 2019-07-10 1.0 3 train foo
# 0 1.0 2019-07-10 1.0 3 test foo #8 df.sort_values() 根据矩阵表中的值进行排序
print(df2.sort_values(by='E')) #对第"E"列的值进行排序
# A B C D E F
# 0 1.0 2019-07-10 1.0 3 test foo
# 2 1.0 2019-07-10 1.0 3 test foo
# 1 1.0 2019-07-10 1.0 3 train foo
# 3 1.0 2019-07-10 1.0 3 train foo

05-11 14:46