博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas基本使用方法和表的合并
阅读量:680 次
发布时间: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/

你可能感兴趣的文章