python處理數(shù)據(jù)利器→Pandas
數(shù)據(jù)一般格式:csv/xlsx
如何用pandas讀取數(shù)據(jù)
案例:用pandas處理商鋪數(shù)據(jù)
用pandas處理
導(dǎo)入模塊
importpandasaspd
#導(dǎo)入pandas模塊
importwarnings
warnings.filterwarnings('ignore')
#不發(fā)出警告
print('成功導(dǎo)入模塊')
#如何用pandas讀取數(shù)據(jù)-csv
df=pd.read_csv('/home/kesci/商鋪數(shù)據(jù).csv')
print(type(df),df['name'].dtype)#查看df類型,查看df中一列的數(shù)值類型
df.head()
#用pandas處理商鋪數(shù)據(jù)-comment字段清洗
df1=df[df['comment'].str.contains('條')]
df1['comment']=df1['comment'].str.split('條').str[0]
print(df1['comment'].dtype)
df1['comment']=df1['comment'].astype('int')
print(df1['comment'].dtype)#更改列數(shù)值類型
df1.head()
#用pandas處理商鋪數(shù)據(jù)-price字段清洗
df1=df1[df1['price'].str.contains('¥')]
df1['price']=df1['price'].str.split('¥').str[-1]
df1['price']=df1['price'].astype('float')
print(df1['price'].dtype)#更改列數(shù)值類型
df1.head()
以上內(nèi)容為大家介紹了如何用python處理數(shù)據(jù),希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。