• 技术文章 >常见问题 >Python常见问题

    如何用python处理数据

    silencementsilencement2020-02-03 17:17:56原创4615

    Python处理数据利器 → Pandas

    数据一般格式:csv/xlsx

    如何用pandas读取数据

    案例:用pandas处理商铺数据

    用pandas处理

    导入模块

    1

    2

    3

    4

    5

    6

    7

    8

    import pandas as pd

        # 导入pandas模块

     

    import warnings

    warnings.filterwarnings('ignore')

        # 不发出警告

     

    print('成功导入模块')

    1

    2

    3

    4

    5

    # 如何用pandas读取数据 - csv

     

    df = pd.read_csv('/home/kesci/商铺数据.csv')

    print(type(df),df['name'].dtype) # 查看df类型,查看df中一列的数值类型

    df.head()

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    # 用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()

    1

    2

    3

    4

    5

    6

    7

    8

    9

    # 用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学习网

    专题推荐:pandas
    上一篇:python如何获取更多时间戳精度 下一篇:python中的map和reduce有什么不同

    相关文章推荐

    • Python数据分析 | pandas汇总和计算描述统计• python中的pandas是什么• python如何导入pandas

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网