如何用python处理数据
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培训机构:开发教育。
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!