python處理數據利器→Pandas
數據一般格式:csv/xlsx
如何用pandas讀取數據
案例:用pandas處理商鋪數據
用pandas處理
導入模塊
importpandasaspd
#導入pandas模塊
importwarnings
warnings.filterwarnings('ignore')
#不發出警告
print('成功導入模塊')
#如何用pandas讀取數據-csv
df=pd.read_csv('/home/kesci/商鋪數據.csv')
print(type(df),df['name'].dtype)#查看df類型,查看df中一列的數值類型
df.head()
#用pandas處理商鋪數據-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)#更改列數值類型
df1.head()
#用pandas處理商鋪數據-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)#更改列數值類型
df1.head()
以上內容為大家介紹了如何用python處理數據,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。