pandas用法大全

2021-08-18 05:57:08 字數 4455 閱讀 4495

一、生成資料表 

1、首先匯入pandas庫,一般都會用到numpy庫,所以我們先導入備用:

import numpy as np

import pandas as pd

df = pd.dataframe(pd.read_csv('name.csv',header=1))

df = pd.dataframe(pd.read_excel('name.xlsx'))

df = pd.dataframe(,

columns =['id','date','city','category','age','price'])

df.shape
df.info()
df.dtypes
df['b'].dtype
df.isnull()
df.isnull()
df['b'].unique()
df.columns
df.head() #預設前10行資料

df.tail() #預設後10 行資料

df.fillna(value=0)
df['prince'].fillna(df['prince'].mean())
df['city']=df['city'].map(str.strip)
df['city']=df['city'].str.lower()
df['price'].astype('int')
df.rename(columns=)
df['city'].drop_duplicates()
df['city'].drop_duplicates(keep='last')
df['city'].replace('sh', 'shanghai')
df1=pd.dataframe()
df_inner=pd.merge(df,df1,how='inner')  # 匹配合併,交集

df_left=pd.merge(df,df1,how='left') #

df_right=pd.merge(df,df1,how='right')

df_outer=pd.merge(df,df1,how='outer') #並集

df_inner.set_index('id')
df_inner.sort_values(by=['age'])
df_inner.sort_index()
df_inner['group'] = np.where(df_inner['price'] > 3000,'high','low')
df_inner.loc[(df_inner['city'] == 'beijing') & (df_inner['price'] >= 4000), 'sign']=1
pd.dataframe((x.split('-') for x in df_inner['category']),index=df_inner.index,columns=['category','size']))
df_inner=pd.merge(df_inner,split,right_index=true, left_index=true)
df_inner.loc[3]
df_inner.iloc[0:5]
df_inner.reset_index()
df_inner=df_inner.set_index('date')
df_inner[:'2013-01-04']
df_inner.iloc[:3,:2] #冒號前後的數字不再是索引的標籤名稱,而是資料所在的位置,從0開始,前三行,前兩列。
df_inner.iloc[[0,2,5],[4,5]] #提取第0、2、5行,4、5列
df_inner.ix[:'2013-01-03',:4] #2013-01-03號之前,前四列資料
df_inner['city'].isin(['beijing'])
df_inner.loc[df_inner['city'].isin(['beijing','shanghai'])]
pd.dataframe(category.str[:3])
df_inner.loc[(df_inner['age'] > 25) & (df_inner['city'] == 'beijing'), ['id','city','age','category','gender']]
df_inner.loc[(df_inner['age'] > 25) | (df_inner['city'] == 'beijing'), ['id','city','age','category','gender']].sort(['age'])
df_inner.loc[(df_inner['city'] != 'beijing'), ['id','city','age','category','gender']].sort(['id'])
df_inner.loc[(df_inner['city'] != 'beijing'), ['id','city','age','category','gender']].sort(['id']).city.count()
df_inner.query('city == ["beijing", "shanghai"]')
df_inner.query('city == ["beijing", "shanghai"]').price.sum()
df_inner.groupby('city').count()
df_inner.groupby('city')['id'].count()
df_inner.groupby(['city','size'])['id'].count()
df_inner.groupby('city')['price'].agg([len,np.sum, np.mean])
df_inner.sample(n=3)
weights = [0, 0, 0, 0, 0.5, 0.5]

df_inner.sample(n=2, weights=weights)

df_inner.sample(n=6, replace=false)
df_inner.sample(n=6, replace=true)
df_inner.describe().round(2).t #round函式設定顯示小數字,t表示轉置
df_inner['price'].std()
df_inner['price'].cov(df_inner['m-point'])
df_inner.cov()
df_inner['price'].corr(df_inner['m-point']) #相關係數在-1到1之間,接近1為正相關,接近-1為負相關,0為不相關
df_inner.corr()
df_inner.to_excel('excel_to_python.xlsx', sheet_name='bluewhale_cc')
df_inner.to_csv('excel_to_python.csv')

pandas用法大全

import numpy as np import pandas as pddf pd.dataframe pd.read csv name.csv header 1 df pd.dataframe pd.read excel name.xlsx df pd.dataframe columns id...

pandas用法大全

import numpy as np import pandas as pddf pd.dataframe pd.read csv name.csv header 1 df pd.dataframe pd.read excel name.xlsx df pd.dataframe columns id...

pandas 用法大全

import numpy as np import pandas as pddf pd.dataframe pd.read csv name.csv header 1 df pd.dataframe pd.read excel name.xlsx df pd.dataframe columns id...