如何用python处理数据

2025-12-02 0 74,340

Python处理数据利器 → Pandas

数据一般格式:csv/xlsx

如何用pandas读取数据

案例:用pandas处理商铺数据

用pandas处理

导入模块

import pandas as pd 
    # 导入pandas模块

import warnings
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学习网!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 常见问题 如何用python处理数据 https://www.zhiup.top/7615.html

相关