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

你可能感兴趣的文章
javaWeb服务详解(含源代码,测试通过,注释) ——Emp的Dao层
查看>>
java实现人脸识别源码【含测试效果图】——Dao层(IUserDao)
查看>>
使用ueditor实现多图片上传案例——前台数据层(Index.jsp)
查看>>
ssh(Spring+Spring mvc+hibernate)——Dept.hbm.xml
查看>>
ssh(Spring+Spring mvc+hibernate)——updateEmp.jsp
查看>>
ssm(Spring+Spring mvc+mybatis)——saveDept.jsp
查看>>
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
查看>>
JavaScript操作BOM对象
查看>>
解决Chrome播放视频闪屏黑屏无法播放
查看>>
Git简单理解与使用
查看>>
echarts 基本图表开发小结
查看>>
二分查找.基于有序数组的查找方法.704
查看>>
制作JS验证码(简易)
查看>>
sklearn :ImportError: cannot import name ‘Imputer‘
查看>>
adb通过USB或wifi连接手机
查看>>
泛型机制 Generic
查看>>
包装类
查看>>
JDK9-15新特性
查看>>
集合继承结构
查看>>
LinkedList 实现类
查看>>