导入Pandas
import numpy as np
import pandas as pd
生成Series对象
s = pd.Series([1,2,3,np.nan,6,8])
//0 1.0
//1 3.0
//2 5.0
//3 NaN
//4 6.0
//5 8.0
生成DataFrame对象
注意:此处的DataFrame和spark中的DataFrame不一样,此处的DataFrame是二维数组的对象。
dates = pd.date_range("20130101",periods=6,freq="3d") //步长为3
//DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04','2013-01-05', '2013-01-06'],dtype='datetime64[ns]', freq='D')
df = pd.DataFrame(np.random.rand(6,4),index=dates,columns=["苹果","香蕉","橘子","西瓜"])
苹果 香蕉 橘子 西瓜
2013-02-01 0.742841 0.620184 0.955725 0.886864
2013-02-04 0.784621 0.260301 0.532879 0.207184
2013-02-07 0.274374 0.448223 0.806035 0.595586
2013-02-10 0.574383 0.997838 0.511176 0.892635
2013-02-13 0.264719 0.820392 0.671498 0.807767
2013-02-16 0.485969 0.364153 0.124876 0.526329
用series字典生成DataFrame
df2 = pd.DataFrame({"A":1,
"B":pd.TimeStamp("20130102"),
"C":pd.Series(1, index=list(range(4)), dtype='float32'),
"D":'D': np.array([3] * 4, dtype='int32'),
"E":pd.Categorical(["test","train","test","train"]),
"F":"foo"
})
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo
注意:以下操作的数据均为上面的df!!!
查看数据
head(n):从上往下取n个,不写n即去尾操作
df.head(2)
苹果 香蕉 橘子 西瓜
2013-02-01 0.742841 0.620184 0.955725 0.886864
2013-02-04 0.784621 0.260301 0.532879 0.207184
tail(n)从下往上取n个,不写n即去头操作
df.tail(2)
苹果 香蕉 橘子 西瓜
2013-02-13 0.264719 0.820392 0.671498 0.807767
2013-02-16 0.485969 0.364153 0.124876 0.526329
显示索引与列名
df.index
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04','2013-01-05', '2013-01-06'],dtype='datetime64[ns]', freq='D')
df.columns
Index(['苹果', '香蕉', '橘子', '西瓜'], dtype='object')
DataFrame转Numpy
提醒:DataFrame.to_numpy()的输出不包含行索引和列标签。
df.to_numpy()
array([[0.742841 0.620184 0.955725 0.886864],
[0.784621 0.260301 0.532879 0.207184,
[0.274374 0.448223 0.806035 0.595586],
[0.574383 0.997838 0.511176 0.892635],
[0.264719 0.820392 0.671498 0.807767],
[0.485969 0.364153 0.124876 0.526329]])
显示各种统计数据结果
df.describe()
苹果 香蕉 橘子 西瓜
count 6.000000 6.000000 6.000000 6.000000
mean 0.572039 0.496039 0.520582 0.434702
std 0.191172 0.308155 0.271576 0.298433
min 0.325867 0.015001 0.194785 0.073450
25% 0.459303 0.311919 0.349174 0.206253
50% 0.554375 0.637600 0.453930 0.433778
75% 0.665315 0.680283 0.744554 0.682968
max 0.867707 0.788166 0.865166 0.770764
按列名的ASCII码排序
df.sort_index(axis=1,ascending=False)
香蕉 西瓜 苹果 橘子
2013-02-01 0.212119 0.073450 0.591529 0.828073
2013-02-04 0.663880 0.770764 0.689911 0.413862
2013-02-07 0.611320 0.244288 0.517221 0.865166
2013-02-10 0.685751 0.702868 0.867707 0.194785
2013-02-13 0.788166 0.623267 0.439997 0.493998
2013-02-16 0.015001 0.193575 0.325867 0.327611
按值排序
df.sort_values(by="香蕉",ascending=False)
苹果 香蕉 橘子 西瓜
2013-02-01 0.194563 0.898953 0.369513 0.111343
2013-02-13 0.110751 0.898615 0.802359 0.358682
2013-02-10 0.658947 0.730751 0.920463 0.010548
2013-02-07 0.957954 0.645569 0.756048 0.059059
2013-02-04 0.438613 0.619301 0.671418 0.732599
2013-02-16 0.303030 0.606781 0.641028 0.712212
选择
获取数据
选择单列值,产生Series
df["苹果"]
苹果
2013-02-01 0.742841
2013-02-04 0.784621
2013-02-07 0.274374
2013-02-10 0.574383
2013-02-13 0.264719
2013-02-16 0.485969
用[ ]切片行:
df[0:3] 或者 df["20130201":"20130207"]
苹果 香蕉 橘子 西瓜
2013-02-01 0.742841 0.620184 0.955725 0.886864
2013-02-04 0.784621 0.260301 0.532879 0.207184
2013-02-07 0.274374 0.448223 0.806035 0.595586
按标签切割
提取第一行数据
df.loc[dates[0]]
苹果 0.469112
香蕉 -0.282863
橘子 -1.509059
西瓜 -1.135632
Name: 2013-01-01 00:00:00, dtype: float64
提取多列数据
df.loc[:,["苹果","香蕉"]]
苹果 香蕉
2013-02-01 0.742841 0.620184
2013-02-04 0.784621 0.260301
2013-02-07 0.274374 0.448223
2013-02-10 0.574383 0.997838
2013-02-13 0.264719 0.820392
2013-02-16 0.485969 0.364153
按行和列切割
df.loc['20130201':'20130207', ['苹果', '香蕉']]
苹果 香蕉
2013-02-01 0.742841 0.620184
2013-02-04 0.784621 0.260301
2013-02-07 0.274374 0.448223
按位置选择
用整数位置选择
df.iloc[3]
2013-02-10
苹果 0.574383
香蕉 0.997838
橘子 0.511176
西瓜 0.892635
df.iloc[3:5,0:2]
苹果 香蕉
2013-02-10 0.574383 0.997838
2013-02-13 0.264719 0.820392
df.iloc[[1,2,4],[0,2]]
苹果 橘子
2013-02-04 0.784621 0.532879
2013-02-07 0.274374 0.806035
2013-02-13 0.264719 0.671498
显示整行切片
df.iloc[:,1:3]
香蕉 橘子
2013-02-01 0.620184 0.955725
2013-02-04 0.260301 0.532879
2013-02-07 0.448223 0.806035
2013-02-10 0.997838 0.511176
2013-02-13 0.820392 0.671498
2013-02-16 0.364153 0.124876
df.iloc[1,1]
0.260301
布尔索引
df[df>0.5]
苹果 香蕉 橘子 西瓜
2013-02-01 0.890646 NaN 0.857915 NaN
2013-02-04 NaN NaN 0.952065 0.565238
2013-02-07 0.634950 NaN NaN 0.987863
2013-02-10 NaN NaN 0.628190 0.791602
2013-02-13 0.973238 NaN 0.996223 0.843551
2013-02-16 0.568098 0.541787 NaN 0.936779
赋值
s1 = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range('20130102', periods=6))
2013-01-02 1
2013-01-03 2
2013-01-04 3
2013-01-05 4
2013-01-06 5
2013-01-07 6