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

你可能感兴趣的文章
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>