pandas
1.导包
import pandas as pd
import numpy as np # 这两个包一般都一起使用
2.结构
1.pandas中主要有两种数据结构,分别是:Series和DataFrame。
2. Series:一种类似于一维数组的对象,是由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。仅由一组数据也可产生简单的Series对象。注意:Series中的索引值是可以重复的。
3. DataFrame:一个表格型的数据结构,包含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型等),DataFrame即有行索引也有列索引,可以被看做是由Series组成的字典。
https://blog.csdn.net/weixin_42107718/article/details/98649208
2-1Series()
s = pd.Series([1,2,3,np.nan,44,1])
print(s)
Example
dates = pd.date_range('20220701',periods=6)
print(dates)
输出
a b c d
2022-07-01 0.344670 -0.881476 -1.195853 0.323223
2022-07-02 -2.077898 0.388571 -0.098368 -0.135736
2022-07-03 -0.862670 -1.246196 0.870801 -0.472193
2022-07-04 -3.700188 0.838331 -0.781934 -0.525130
2022-07-05 -0.735568 0.476634 -0.013343 0.341969
2022-07-06 1.338018 -0.300296 -1.080252 -0.495870
2-2 DataFrame()
#n代表normal distribution正态分布,所以randn是在正态分布中随机取值,随机生成x行y列的数组
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
print(df)
print("............................................................................\n")
3.DataFrame()
3-1DataFrame的建立 DataFrame()
Example1:
df1 = pd.DataFrame(np.arange(12).reshape((3,4)))
print(df1)
print("............................................................................\n")
df2 = pd.DataFrame({'A':1.,
'B':pd.Timestamp('20220701'),
'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':'fool'})
print(df2)
print(df2.dtypes)
print(df2.columns)
print(df2.values)
print("............................................................................\n")
print(df2)
print(df2.describe())
'''
describle()的输出为;
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
含义:
1.count:非空值总数
2.mean:非空值的平均值
3.std:方差
4.min:最小值
5. 25%:25%分位数
6. 50%:50%分位数(中位数)
7. 75%: 75%分位数
8.max: 最大值
9.dtype: 数据类型
'''
Example2:
dates = pd.date_range('20220701',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
print(df.sort_index(axis=1,ascending=False))
print(df.sort_index(axis=0,ascending=False))
print("............................................................................\n")
dates = pd.date_range("20220702",periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['A','B','C','D'])
print(df)
3-2正序和倒序
# axis=1 是横向排序,axis=0 是纵向排序 False是倒序 True是正序
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
print(df.sort_index(axis=1,ascending=False))
print(df.sort_index(axis=0,ascending=False))
print("............................................................................\n")
3-3切片操作
1 横向切片
print(df[0:3])
2 根据字符串进行切片
print(df['20220702':'20220704'])
3通过标签切片
# select by label:loc
print(df)
print("............................................................................\n")
print(df.loc['20220702'])
# select 列向标签
print(df.loc[:,['A','B']])
# 行列都进行索引
print(df.loc['20220702',['A','B']])
# select by position:iloc
print(df.iloc[3,1])
print(df.iloc[[1,3,5],1:3])
print('s',df[df.A<13])
4pandas设置值
df.iloc[2,2] = 111
df.loc['20220702','B'] = 222
df.A[df.A>4] = 0
df['E'] = pd.Series([1,2,3,4,5,6],index=pd.date_range('20220702',periods=6))
print(df)
5 pandas处理丢失数据dropna()方法-丢弃含空值的行、列
# pandas 处理丢失数据
print("............................................................................\n")
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])
df.iloc[0,1] = np.nan
df.iloc[1,2] = np.nan
print('1',df)
print(df.dropna(axis=0,how='any'))
how = {'any','all'}
when how = 'any',只要有一个出现nan,则这行或者这列删除;
when how = 'all',只有全部出现nan,这行或者这列才删除
6判断DataFrame中是否有null df.isnull()
print(df.isnull())
print(np.any(df.isnull()) == True) # 是否至少有一个True
7pandas中的DataFrame拼接
7-1connection连接功能
# connection
# ignore index = True 表示目录顺移
df1 = pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'])
df2 = pd.DataFrame(np.ones((3,4))*1,columns=['a','b','c','d'])
df3 = pd.DataFrame(np.ones((3,4))*2,columns=['a','b','c','d'])
df = pd.concat([df1,df2,df3],axis=0,ignore_index='True')
'''
axis=0/1 0为纵向connection
'''
print('df1',df1)
print("----------------------------------------df1---------------------------------------------------------------------")
print('df2',df2)
print("----------------------------------------df2---------------------------------------------------------------------")
print('df3',df3)
print("-------------------------------------------------------------------------------------------------------------")
print('df',df)
7-2contact添加功能
# join关键字,['inner','outer']
'''
'inner'与'outer'区别:
# join, ('inner', 'outer')
df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3])
df2 = pd.DataFrame(np.ones((3,4))*1, columns=['b','c','d', 'e'], index=[2,3,4])
res = pd.concat([df1, df2], axis=1, join='outer')
print("\n")
print(res)
res = pd.concat([df1, df2], axis=1, join='inner')
print("\n")
print(res)
结果:
a b c d b c d e
1 0.0 0.0 0.0 0.0 NaN NaN NaN NaN
2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
4 NaN NaN NaN NaN 1.0 1.0 1.0 1.0
从结果上看:outer是全部都加入
a b c d b c d e
2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
3 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
从结果上看:inner是其只增加相同的
'''
df4 = pd.DataFrame(np.ones((3,4))*1,columns=['b','c','d','e'],index=[2,3,4])
print(df1)
print("--------------------------------------df1-----------------------------------------------------------------------")
print(df4)
print("--------------------------------------df4-----------------------------------------------------------------------")
res = pd.concat([df1,df2],join='outer',ignore_index=True)
print(res)
print("--------------------------------------res-------------------------- --------------------------------------------")
res = pd.concat([df1,df2],axis=1)
print(res)
7-3pandas合并merge()
# pandas合并 merge
# simple example
left = pd.DataFrame({'key':['K0','K1','K2','K3'],
'A':['A0','A1','A2','A3'],
'B':['B0','B1','B2','B3']})
right = pd.DataFrame({'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3'],
'key':['K0','K1','K2','K3']})
print(left)
print(right)
print("============================")
res = pd.merge(left,right,on='key') # on = 'key' 表示基于key合并,只合并相同的部分
print(res)
7-4how关键字
left = pd.DataFrame({'key1':['K0','K1','K2','K3'],
'key2':['K0','K1','K0','K1'],
'A':['A0','A1','A2','A3'],
'B':['B0','B1','B2','B3']})
right = pd.DataFrame({'key1':['K0','K1','K1','K2'],
'key2':['K0','K0','K0','K0'],
'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3'],})
# how ={'left','right','outer','inner'}
res = pd.merge(left,right,on=['key1','key2']) # 其实就是默认 how='inner'合并相同的,
res1= pd.merge(left,right,on=['key1','key2'],how='outer') # how = 'outer'合并不同的
print(res)
print("===========left===============")
print(left)
print("===========right==============")
print(right)
7-5indicator关键字:
# # indicator
# df1 = pd.DataFrame({'col1':[0,1],'col_left':['a','b']})
# df2 = pd.DataFrame({'col1':[1,2,2],'co1_right':[2,2,2]})
# print(df1)
# print(df2)
# res = pd.merge(df1,df2,on='col1',how='outer',indicator=True) # 直观显示
# print(res)
7-6merge by index
# merge by index
left = pd.DataFrame({'A':['A0','A1','A2'],
'B':['B0','B1','B2']},
index=['K0','K1','K2'])
right = pd.DataFrame({'C':['C0','C2','C3'],
'D':['D0','D2','D3']},
index=['K0','K2','K3'])
print(left)
print(right)
res = pd.merge(left,right,left_index=True,right_index=True,how='outer',indicator=True)
print(res)
7-7规定用哪个目录进行合并
boys =pd.DataFrame({'k':['k0','k1','k2'],'age':[1,2,3]})
girls = pd.DataFrame({'k':['k0','k0','k3'],'age':[4,5,6]})
print(boys)
print(girls)
res = pd.merge(boys,girls,on='k',suffixes=['_boys','_girl'],how='inner',indicator=True)
res1 = pd.merge(boys,girls,on='k',suffixes=['_boys','_girl'],how='outer',indicator=True)
print(res)
print(res1)
8 对文件进行操作
8-1读取文件
# 读取文件
df = pd.read_csv('nba.csv')
print(df.to_string())
8-2存储文件
# 存储文件
# 三个字段 name, site, age
nme = ["Google", "Runoob", "Taobao", "Wiki"]
st = ["www.google.com", "www.runoob.com", "www.taobao.com", "www.wikipedia.org"]
ag = [90, 40, 80, 98]
# 字典
dict = {'name': nme, 'site': st, 'age': ag}
df = pd.DataFrame(dict)
# 保存 dataframe
df.to_csv('site.csv')
print("--------------------------------------------------------")
print("--------------------------------------------------------")
print(df)
9pandas plot
9-1折线图
data = pd.Series(np.random.randn(1000),index=np.arange(1000))
data =data.cumsum() # 生成数据的累加
data.plot()
plt.show()
data = pd.DataFrame(np.random.randn(1000, 4), index=np.arange(1000), columns=list("ABCD"))
data = data.cumsum()
data.plot()
plt.show()
9-2 散点图
data = pd.DataFrame(np.random.randn(1000, 4), index=np.arange(1000), columns=list("ABCD"))
ax = data.plot.scatter(x='A', y='B', color='DarkBlue', label="Class 1")
data.plot.scatter(x='A', y='C', color='LightGreen', label='Class 2', ax=ax)
plt.show()
笔记中的函数总结
https://blog.csdn.net/weixin_45046542/article/details/125603619?spm=1001.2014.3001.5502