pandas用法總結(jié)

#1、數(shù)據(jù)的生成(dataframe數(shù)據(jù)框|series序列)

#模塊導(dǎo)入

import pandas as pd

import numpy as np

import pymysql

#方法1:excel、csv、sql導(dǎo)入

df_exl=pd.read_excel('C:/Users/15432/Desktop/testdata_1.xlsx',header=0)#導(dǎo)入excel文件從0行開(kāi)始

df_exl2=pd.read_excel('C:/Users/15432/Desktop/testdata_2.xlsx')

df_csv=pd.read_csv('C:/Users/15432/Desktop/testdata_1.csv')

db = pymysql.connect("127.0.0.1","root","123456","mysql" )# 打開(kāi)數(shù)據(jù)庫(kù)連接:主機(jī),賬號(hào),密碼,數(shù)據(jù)庫(kù)名稱

cursor = db.cursor()# 使用 cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor

sql="""SELECT VERSION()"""

cursor.execute(sql)#使用 execute()? 方法執(zhí)行 SQL 查詢

data = cursor.fetchone()# 使用 fetchone() 方法獲取單條數(shù)據(jù).

print ("Database version : %s " % data)

db.close()# 關(guān)閉數(shù)據(jù)庫(kù)連接

#方法2:自己創(chuàng)建dataframe數(shù)據(jù)框或series序列

series=pd.Series([1,2,3,4,np.nan,6,7])

print(series)

dates=pd.date_range('20180601',periods=3)

df1=pd.DataFrame(np.random.randn(3,4),index=dates,columns=['one','two','three','four'])#numpy隨機(jī)數(shù)生成

df2=pd.DataFrame({'id':[1001,1002,1003],'city':['bj','sh','gz'],'age':['22','23','34']})#字典生成

print(df1)

df2


2、數(shù)據(jù)表信息查看

(1)數(shù)據(jù)表的維度查看

df_exl.shape

(2)數(shù)據(jù)表基本信息(維度、列名稱、數(shù)據(jù)格式、所占空間等)

df_exl.info

(3)查看數(shù)據(jù)表的值

df_exl.values

(4)查看列名稱

df_exl.columns

#(5)查看前5行數(shù)據(jù)、后5行數(shù)據(jù):

df_exl.head()

df_exl.tail()

(6)查詢某一列格式

df_exl['用戶編號(hào)'].dtype

(7)查詢空值

df_exl.isnull()

df_exl.isna()

(8)查看某一列的唯一值

consumer=df_exl['客戶名'].unique()

counter=0

for i in consumer:

? ? counter+=1

print(counter)

consumer

3、數(shù)據(jù)清洗

(1)用數(shù)字0填充空值

df_exl.fillna(value=0)

df_exl.dropna(axis=1,how='all')#刪除整列都是na值的整列刪除

(2) 用201805這列的均值填充空值

df_exl['201805'].fillna(df_exl['201805'].mean())

(3)將數(shù)據(jù)表某列或某行刪除

df_exl=df_exl.drop(axis=1,columns='201806')

df_exl=df_exl.drop(axis=0,index='')

(4)清除某列的字符空格

df_exl['客戶名']=df_exl['客戶名'].map(str.strip)

(5)大小寫轉(zhuǎn)換

df_exl['客戶名']=df_exl['客戶名'].str.lower()

df_exl['客戶名']=df_exl['客戶名'].str.upper()

(6)更改數(shù)據(jù)格式

df_exl['入網(wǎng)時(shí)間'].astype('int64')

(7)刪除先出現(xiàn)的重復(fù)值

df_exl['用戶編號(hào)'].drop_duplicates()

刪除后出現(xiàn)的重復(fù)值

df_exl['用戶編號(hào)'].drop_duplicates(kepp='last')

(8)數(shù)據(jù)替換

df2['city'].replace('bj','sh')#sh替換bj

3、數(shù)據(jù)預(yù)處理

#刪除201806這列

df_exl=df_exl.drop(axis=1,columns='201806')

df_exl2=df_exl2.drop(axis=1,columns='201806')

#(1)數(shù)據(jù)表合并

#交集形式

df_inner=pd.merge(df_exl,df_exl2,how='inner',on='用戶編號(hào)')

#并集形式

df_outer=pd.merge(df_exl,df_exl2,how='outer',on=['用戶編號(hào)','設(shè)備號(hào)','中心','執(zhí)行人','入網(wǎng)時(shí)間','客戶名'])

#(3)設(shè)置索引列

df_exl.set_index('用戶編號(hào)')

#(4)按索引列排序

df_exl.sort_index()

#(5)按特定列的值排序

df_exl.sort_values(by=['用戶編號(hào)'])

#(6)如果用戶編號(hào)列的值大于1000000000000000,則客戶名列顯示high,否則顯示low

df_exl['客戶名']=np.where(df_exl['用戶編號(hào)']>1000000000000000,'high','low')

#df_exl['客戶名']

#對(duì)復(fù)合多個(gè)條件的數(shù)據(jù)進(jìn)行分組標(biāo)記

df_exl.loc[(df_exl['201801']==0) & (df_exl['201803']>=20),'sign']=1

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容