目錄
一、生成數(shù)據(jù)表
1.導(dǎo)入pandas庫
2.導(dǎo)入CSV或者xlsx文件
3.用pandas創(chuàng)建數(shù)據(jù)表
二、數(shù)據(jù)表信息查看
1.維度查看
2.數(shù)據(jù)表基本信息(維度、列名稱、數(shù)據(jù)格式、所占空間等)
3.每一列數(shù)據(jù)的格式
4.某一列格式
5.空值判斷
6.查看某一列空值
7.查看某一列的唯一值
8.查看數(shù)據(jù)表的值
9.查看列名稱
10.查看前5行數(shù)據(jù)、后5行數(shù)據(jù)
三、數(shù)據(jù)表清洗
1.用數(shù)字0填充空值
2.使用列prince的均值對NA進(jìn)行填充
3.清除city字段的字符空格
4.大小寫轉(zhuǎn)換
5.更改數(shù)據(jù)格式
6.更改列名稱
7.刪除后出現(xiàn)的重復(fù)值
8.刪除先出現(xiàn)的重復(fù)值
9、數(shù)據(jù)替換
四、數(shù)據(jù)預(yù)處理
1.數(shù)據(jù)表合并
2.設(shè)置索引列
3.按照特定列的值排序
4.按照索引列排序
5.如果prince列的值>3000,group列顯示high,否則顯示low.
?6.對復(fù)合多個條件的數(shù)據(jù)進(jìn)行分組標(biāo)記
7.對category字段的值依次進(jìn)行分列,并創(chuàng)建數(shù)據(jù)表,索引值為df_inner的索引列,列名稱為category和size?
?8.將完成分裂后的數(shù)據(jù)表和原df_inner數(shù)據(jù)表進(jìn)行匹配
五、數(shù)據(jù)提取
六、數(shù)據(jù)篩選
七、數(shù)據(jù)匯總
八、數(shù)據(jù)統(tǒng)計
九、數(shù)據(jù)輸出文章來源:http://www.zghlxwxcb.cn/news/detail-488592.html
一、生成數(shù)據(jù)表
1.導(dǎo)入pandas庫
import pandas as pd
2.導(dǎo)入CSV或者xlsx文件
df = pd.DataFrame(pd.read_csv('filename.csv'))
df = pd.DataFrame(pd.read_excel('filename.xlsx'))
3.用pandas創(chuàng)建數(shù)據(jù)表
import numpy as np
df = pd.DataFrame({"id":[1001,1002,1003,1004,1005,1006],
"date":pd.date_range('20130102', periods=6),
"city":['Beijing ', 'SH', ' guangzhou ', 'Shenzhen', 'shanghai', 'BEIJING '],
"age":[23,44,54,32,34,32],
"category":['100-A','100-B','110-A','110-C','210-A','130-F'],
"price":[1200,np.nan,2133,5433,np.nan,4432]},
columns =['id','date','city','category','age','price'])
二、數(shù)據(jù)表信息查看
1.維度查看
df.shape
df.shape
Out[36]: (6, 6)
2.數(shù)據(jù)表基本信息(維度、列名稱、數(shù)據(jù)格式、所占空間等)
df.info()
3.每一列數(shù)據(jù)的格式
df.dtypes
4.某一列格式
df['date'].dtype
df['price'].dtype
5.空值判斷
df.isnull()?
6.查看某一列空值
7.查看某一列的唯一值
8.查看數(shù)據(jù)表的值
df.values
9.查看列名稱
df.columns
10.查看前5行數(shù)據(jù)、后5行數(shù)據(jù)
df.head() #默認(rèn)前5行數(shù)據(jù)
df.tail() #默認(rèn)后5行數(shù)據(jù)
三、數(shù)據(jù)表清洗
1.用數(shù)字0填充空值
df.fillna(value=0)
2.使用列price的均值對NA進(jìn)行填充
df['price'].fillna(df['price'].mean())
3.清除city字段的字符空格
df['city']=df['city'].map(str.strip)
4.大小寫轉(zhuǎn)換
df['city']=df['city'].str.lower()
5.更改數(shù)據(jù)格式
df['price'].astype('int')
6.更改列名稱
df.rename(columns={'category': 'category-size'})?
7.刪除后出現(xiàn)的重復(fù)值
df['city'].drop_duplicates()
8.刪除先出現(xiàn)的重復(fù)值
df['city'].drop_duplicates(keep='last')
9、數(shù)據(jù)替換
df['city'].replace('sh', 'shanghai')
四、數(shù)據(jù)預(yù)處理
df1=pd.DataFrame({"id":[1001,1002,1003,1004,1005,1006,1007,1008],
? ? ? ? ? ? ? ? ? "gender":['male','female','male','female','male','female','male','female'],
? ? ? ? ? ? ? ? ? "pay":['Y','N','Y','Y','N','Y','N','Y',],
? ? ? ? ? ? ? ? ? "m-point":[10,12,20,40,40,40,30,20]})
1.數(shù)據(jù)表合并
(1)merge
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') ?#并集
(2)append
result = df1.append(df2)
df_result = df3.append(df4)
df3 = pd.DataFrame({'A':['A0','A1','A2','A3'],
'B':['B0','B1','B2','B3'],
'C':['C0','C1','C2','C3']})
df4 = pd.DataFrame({'A':['A4','A5','A6','A7'],
'B':['B4','B5','B6','B7'],
'C':['C4','C5','C6','C7']})
df_result = df3.append(df4)
?(3)join
result = left.join(right, on='key')
(4)concat?
df5 = pd.DataFrame({'A':['A4','A5','A6','A7'],
'B':['B4','B5','B6','B7'],
'C':['C4','C5','C6','C7']})
result = pd.concat([df3,df4,df5])
2.設(shè)置索引列
result.set_index('A')
3.按照特定列的值排序
df_inner.sort_values(by=['age'])
4.按照索引列排序
?df=result.set_index('A')
df.sort_index()
5.如果prince列的值>3000,group列顯示high,否則顯示low.
df_inner['group'] = np.where(df_inner['price'] > 3000,'high','low')
6.對復(fù)合多個條件的數(shù)據(jù)進(jìn)行分組標(biāo)記
?df.loc[(df['city'] == 'beijing') & (df['price'] >= 4000), 'sign']=1
7.對category字段的值依次進(jìn)行分列,并創(chuàng)建數(shù)據(jù)表,索引值為df_inner的索引列,列名稱為category和size?
pd.DataFrame((x.split('-') for x in df_inner['category']),index=df_inner.index,columns=['category','size'])
8.將完成分裂后的數(shù)據(jù)表和原df_inner數(shù)據(jù)表進(jìn)行匹配
?df_inner=pd.merge(df_inner,df_split,right_index=True, left_index=True)
五、數(shù)據(jù)提取
六、數(shù)據(jù)篩選
七、數(shù)據(jù)匯總
八、數(shù)據(jù)統(tǒng)計
九、數(shù)據(jù)輸出
————————————————
參考鏈接:https://blog.csdn.net/yiyele/article/details/80605909文章來源地址http://www.zghlxwxcb.cn/news/detail-488592.html
到了這里,關(guān)于【Pandas】pandas用法解析(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!