import numpy as np
import pandas as pd
df1=pd.DataFrame(np.arange(9).reshape(3,3),index=['cs','sh','bj'],columns=['a','b','c'])
#获取索引
print(df1.index)
#修改索引
df1.index=['shanghai','changshang','beijing']
print(df1)
#修改列名
df1.columns=['A','B','C']
print(df1)
#批量进行重命名
def func(x):
return x+'_abc' #注意此时x类型应为字符串
df1.rename(index=func,columns=func,inplace=True) #没有inplace=true时,不会修改df1的数据,而是返回一个新的数据
print(df1)
#只更改几个
df1.rename(index={'shanghai_abc':'SHANGHAI'},columns={'A_abc':'AAA'},inplace=True)
print(df1)
#添加列
df1['cost']=[50,60,1000]
print(df1)
#合并
#concat()#只会进行简单的拼接 有一样的不会匹配
df2=pd.DataFrame(np.arange(6).reshape(3,2),columns=['a','b'])
df3=pd.DataFrame(np.arange(6).reshape(2,3),columns=['c','d','e'])
res=pd.concat([df2,df3]) #根据0轴拼接 直接加在后面
print(res)
res2=pd.concat([df2,df3],axis=1) #根据index轴拼 根据1轴拼接
print(res2)
#merge()
df4=pd.DataFrame({'key':['K0','K1','K2','K3'],
'A':['A0','A1','A2','A3'],
'B':['B0','B3','B1','B2']})
df5=pd.DataFrame({'key':['K0','K1','K2','K3'],
'C':['C0','C1','C2','C3'],
'D':['D0','D1','D2','D3']})
df8=pd.merge(df4,df5,on='key')
print(df8)
#pd.merge(user,buy,on='',how='') how里关键字:left right inner ...
#join()
#设置索引
df6=df4.set_index('key')
df7=df5.set_index('key')
jo=df6.join(df7,on='key')
print(jo)
#数据删除 删除时axis=0 表示按行删除 axis=1 表示案列删除
df9=df8.drop(0)
print(df9)
df10=df8.drop('A',axis=1)
print(df10)
#数据查询
#querry()
print(df2)
print(df2.query('a <2'))
#isin() 是否包含
df11=df5['key'].isin(['K3'])
print(df11)
#展开为多行多列
data={
'A':[1,2,3],
'B':[['x','y'],['p','q','r'],['m']]
}
df12=pd.DataFrame(data)
df12[['B1','B2','B3']]=df12['B'].apply(pd.Series)
print(df12)
#展开为多行
df13=pd.DataFrame(data)
df13=df13.explode('B')
print(df13)
pandas数据处理
最新推荐文章于 2024-11-08 13:43:44 发布