博客
关于我
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/

你可能感兴趣的文章
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>