欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

pandas笔记

时间:2023-04-20

import numpy as npimport pandas as pds = pd.Series([1,3,6,np.nan,44,1]) #seriesdates = pd.date_range('20160101',periods=6) #从20160101开始的6天df = pd.Dataframe(np.random.randn(6,4), index=dates, columns=['a','b','c','d']) #index为行索引,columns为栏index = df.index #返回索引(行号)columns = df.columns #返回栏(列号)values = df.values #所有的数返回一个arraydescribe = df.describe() #各种统计参数T = df.Tsort_index = df.sort_index(axis=0, ascending=False) #排序索引,axis=0为对index,ascending上升的sort_value = df.sort_values(by='a') #对value的a列进行排序df.reset_index(drop=True, inplace=True) #对索引进行重排df['a'] #只要a列df[0:3] #对行进行切片df['20160101':'20160104'] #同对行进行切片df.loc['20160102'] #定位行df.loc[:,['a','b']] #定位列df.iloc[3:5, 1:3]#根据位置切片df[df.a>0.5] #筛选行的数值df.b[df.a>0] = 0 #a列大于4的数 b列置零#值的设置dates = pd.date_range('20130101', periods=6)df = pd.Dataframe(np.arange(24).reshape((6,4)), index=dates, columns=['A','B','C','D'])df.iloc[2,2] = 1111 #修改元素df['F'] = np.nandf['E'] = pd.Series([1,2,3,4,5,6],index = pd.date_range('20130101',periods=6))#处理丢失数据dates = pd.date_range('20130101',periods=6)df = pd.Dataframe(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])df.iloc[0,1] = np.nandf.iloc[1,2] = np.nandropna = df.dropna(axis=0,how='any')#axis=0为行 how={'any','all'}np.any(df.isnull()) #任何一个isnull是true则返回true,即检验里面是否有nanfillna = df.fillna(value=0) #填充nan#导入导出#data = pd.read_csv('path.csv') #读完之后直接生成dataframe#data.to_pickle('student.pickle')#pandas合并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'])res = pd.concat([df1,df2,df3], axis=0, ignore_index=True) #0为y轴方向,1为x轴方向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],join='inner',ignore_index=True) #join表示合并方式,取并集、交集outer、innerres = df1.append(df2, ignore_index=True) #向下叠加#merge的合并left = pd.Dataframe({'key':['K0','K1','K2','K3'], 'A':['A0','A1','A2','A3'], 'B':['B0','B1','B2','B3']})right = pd.Dataframe({'key':['K0','K1','K2','K3'], 'C':['C0','C1','C2','C3'], 'D':['D0','D1','D2','D3']})res = pd.merge(left, right, on='key') #合并left = pd.Dataframe({'key1':['K0','K0','K1','K2'], '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']})res = pd.merge(left,right,on=['key1','key2'],how='right') #on是基于哪两列,合并 how = 'inner','outer','right','left'print(res)

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。