大家吼哇,我又来了。今天我要讲讲这个,让所有人一用就爱上的Python库——Pandas。它为了解决数据分析问题而生,它纳入了各种库和标准数据类型,对于我们处理数据文件而言,可以说是非常方便了。它能够处理很多多维数据,一维数据Series,二位数据DataFrame,三维数据Panel,四维数据Panel4D,多维数据PanelND。接下来,我们来具体实践一下Pandas的使用吧~
import pandas as pd
import numpy as np
df=pd.DataFrame(pd.read_excel(r"data\test.xlsx",header=1))#导入excel文件
print(df.shape)#查看数据的维度
#运行结果(9, 12)
print(df.info)#查看数据的基本信息
#运行结果:
<bound method DataFrame.info of 1 2
0 1.0 3.0
1 1.0 4.0
2 NaN 5.0
3 1.0 6.0
4 1.0 7.0
5 1.0 NaN
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0>
print(df.dtypes)#查看每列的数据类型
#运行结果
1 float64
2 float64
dtype: object
df.columns=["first","second"]#设置列名
print(df.columns.values)#查看数据表的列名
#运行结果:['first' 'second']
print(df.head())#查看表格的前5行数据
print(df.tail())#查看表格的后5行数据
#运行结果:
first second
0 1.0 3.0
1 1.0 4.0
2 NaN 5.0
3 1.0 6.0
4 1.0 7.0
first second
4 1.0 7.0
5 1.0 NaN
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0
print(df.values)#查看数据表的值
#运行结果:
[[ 1. 3.]
[ 1. 4.]
[nan 5.]
[ 1. 6.]
[ 1. 7.]
[ 1. nan]
[ 1. 9.]
[ 1. 10.]
[ 1. 11.]]
print(df["first"].dtype)#查看某一列的数据类型
#运行结果:float64
print(df.isnull())#判断值是否为空
#运行结果:
first second
0 False False
1 False False
2 True False
3 False False
4 False False
5 False True
6 False False
7 False False
8 False False
print(df["second"].unique())#查看某一列的唯一值
#运行结果:[ 3. 4. 5. 6. 7. nan 9. 10. 11.]
print(df.fillna(value=0))#填充空值
#运行结果:
first second
0 1.0 3.0
1 1.0 4.0
2 0.0 5.0
3 1.0 6.0
4 1.0 7.0
5 1.0 0.0
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0
df=df.fillna(value=0)#填充空值
print(df["first"].astype(int))#更改数据类型
#运行结果:
0 1
1 1
2 0
3 1
4 1
5 1
6 1
7 1
8 1
Name: first, dtype: int32
df=df.rename(columns={"first":"First"})#更改列名
#运行结果:
First second
0 1.0 3.0
1 1.0 4.0
2 0.0 5.0
3 1.0 6.0
4 1.0 7.0
5 1.0 0.0
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0
print(df["first"].drop_duplicates())#删除后出现的重复值
#运行结果:
0 1.0
2 0.0
print(df['first'].drop_duplicates(keep='last'))#删除先出现的重复值
#运行结果:
2 0.0
8 1.0
Name: first, dtype: float64
print(df['first'].replace(1, 'nihao'))#元素值替换
#运行结果:
0 nihao
1 nihao
2 0
3 nihao
4 nihao
5 nihao
6 nihao
7 nihao
8 nihao
Name: first, dtype: object
df.set_index("second")
print(df.sort_values(by=['second']))#按照列的值进行排序
#运行结果:
first second
5 1.0 0.0
0 1.0 3.0
1 1.0 4.0
2 0.0 5.0
3 1.0 6.0
4 1.0 7.0
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0
df1.columns=["first","second"]
print(df.append(df1))#将另一个数据表中的数据加入到前一个数据表中
#运行结果:
first second
0 1.0 3.0
1 1.0 4.0
2 0.0 5.0
3 1.0 6.0
4 1.0 7.0
5 1.0 0.0
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0
0 1.0 3.0
1 1.0 4.0
2 NaN 5.0
3 1.0 6.0
4 1.0 7.0
5 1.0 NaN
6 1.0 9.0
7 1.0 10.0
8 1.0 11.0
print(df.join(df1))#向右合并两个表
#运行结果:
first second 1 2
0 1.0 3.0 1.0 3.0
1 1.0 4.0 1.0 4.0
2 0.0 5.0 NaN 5.0
3 1.0 6.0 1.0 6.0
4 1.0 7.0 1.0 7.0
5 1.0 0.0 1.0 NaN
6 1.0 9.0 1.0 9.0
7 1.0 10.0 1.0 10.0
8 1.0 11.0 1.0 11.0
除了上面的内容,还有很重要的一部分,对数据的操作部分,这可是对数据分析不可避免的一部分,一起来get!
df.set_index("second")#在second列设置索引
df.sort_index()#按照索引列进行排序
df["s"] = np.where(df["second"] > 3,"high","low")#如果second的值大于3,second列显示high,否则显示low
print(df["s"])
#运行结果:
0 low
1 high
2 high
3 high
4 high
5 low
6 high
7 high
8 high
Name: s, dtype: object
数据提取:
print(df.loc[3])#按索引提取数据
#运行结果:
first 1.0
second 6.0
sign 1.0
Name: 3, dtype: float64
print(df.iloc[0:5])#按索引提取区域数据
first second sign
0 1.0 3.0 1.0
1 1.0 4.0 1.0
2 0.0 5.0 NaN
3 1.0 6.0 1.0
4 1.0 7.0 1.0
print(df.iloc[:3,:1])#iloc按区域提取数据,前3行,前1列
#运行结果:
first
0 1.0
1 1.0
2 0.0
print(df.iloc[[0,2,5],[0,1]])#按iloc单独提取行列数据,第0、2、5行,第0、1列
#运行结果:
first second
0 1.0 3.0
2 0.0 5.0
5 1.0 0.0
print(df["second"].isin([1,2,3,4,5]))#判second列的数据是否在某个集合内
#运行结果:
0 True
1 True
2 True
3 False
4 False
5 False
6 False
7 False
8 False
Name: second, dtype: bool
啊,我好累,写不动了,下一篇再见~