博客
关于我
pandas基本使用方法和表的合并
阅读量:682 次
发布时间:2019-03-17

本文共 1530 字,大约阅读时间需要 5 分钟。

import pandas as pd

def basic_pd():
    # pandas get data of excel
    path = "D:/test/test.csv"

    # Reading csv is similar to reading excel

    df = pd.DataFrame(pd.read_csv(path, header=0))
    # 如果含有中文一定要设置编码方式
    # df = pd.DataFrame(pd.read_csv(path, header=0, encoding='gbk'))

    # df = pd.DataFrame(pd.read_excel(path, header=0))

    # Show the information of excel
    print("Excel的文件信息:")
    print(df.info())

    print("提取Excel中的第一行数据:")

    # 返回第一行的一维列表
    for key in df.keys():
        print(key)
    # 返回除去第一行的二维列表
    print("提取Excel中的第一行数据:")
    for value in df.values:
        print(value)

    count_row = df.count(axis=1)

    print("统计Excel中的每一行的字段数量:")
    print(count_row)

    print("统计Excel中的每一列的字段数量:")

    count_column = df.count(axis=0)
    print(count_column)

    print("提取统计字段的key:")

    for key in count_column.keys():
        print(key)
    print("提取统计字段的values:")
    for value in count_column.values:
        print(value)

def table_pd():
    path = "D:/test/test.csv"
    path1 = "D:/test/test1.csv"

    df_table = pd.DataFrame(pd.read_csv(path, header=0))

    df_table1 = pd.DataFrame(pd.read_csv(path1, header=0, encoding='gbk'))

    # 连接的过程是以左右表中左侧第一列值为id

    print("内连接:提取左右表id相同的数据")
    df_inner = pd.merge(df_table, df_table1, how='inner')
    print(df_inner)

    print("左连接:提取左表中全部数据,右表数据补充左表中id相同的字段和数据")

    df_left = pd.merge(df_table, df_table1, how='left')
    print(df_left)

    print("右连接:提取右表中全部数据,左表数据补充右表中id相同的字段和数据")

    df_right = pd.merge(df_table, df_table1, how='right')
    print(df_right)

    print("左右表相互补充字段,合并id相同的数据")

    df_outer = pd.merge(df_table, df_table1, how='outer')
    print(df_outer)

if __name__ == '__main__':
    # pandas的基本使用
    basic_pd()
    # pandas的表的合并
    table_pd()

转载地址:http://ovgqz.baihongyu.com/

你可能感兴趣的文章
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>
Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
查看>>
nginx最最最详细教程来了
查看>>
Nginx服务器---正向代理
查看>>
Nginx服务器上安装SSL证书
查看>>
Nginx服务器基本配置
查看>>
Nginx服务器的安装
查看>>
Nginx标准配置文件(包括反向代理、大文件上传、Https证书配置、文件预览等)
查看>>
Nginx模块 ngx_http_limit_conn_module 限制连接数
查看>>
Nginx模块 ngx_http_limit_req_module 限制请求速率
查看>>
nginx添加允许跨域header头
查看>>
nginx添加模块与https支持
查看>>
nginx状态监控
查看>>
Nginx用户认证
查看>>
Nginx的location匹配规则的关键问题详解
查看>>
Nginx的Rewrite正则表达式,匹配非某单词
查看>>
Nginx的使用总结(一)
查看>>