day06-pandas高阶 课件代码上午

01-数据合并

import pandas as pd

"""
import numpy as np
numpy 合并数组
np.hstack() # 水平合并
np.vstack() # 垂直合并
np.concatenate() # 行的方向 axis = 0
np.concatenate() #  列的方向 axis = 1
"""

# # 获取df数据
# df1 = pd.read_excel('./直接拼接数据.xlsx', sheetname=0)
# df2 = pd.read_excel('./直接拼接数据.xlsx', sheetname=1)
# print('df1:\n', df1)
# print('df2:\n', df2)
# print('*' * 100)

# 直接拼接 ---pd.concat
# axis=0,join='outer' 行的方向上外连接
# 行上直接拼接,列的方向上求并集,如果没有值的地方用NaN补齐
# res = pd.concat((df1, df2), axis=0, join='outer')
# print('res:\n', res)
# axis=0,join = 'inner' ,行的方向上内连接
# 行上直接拼接,列的方向上求交集
# res = pd.concat((df1, df2), axis=0, join='inner')
# print('res:\n', res)

# axis=1,join='outer' 列的方向上外连接
# 列上直接拼接,行的方向上求并集,如果没有值的地方用NaN来补齐
# res = pd.concat((df1, df2), axis=1, join='outer')
# print('res:\n', res)
# axis=1, join='inner' ,列的方向上内连接
# 列上直接拼接,行的方向上求交集
# res = pd.concat((df1, df2), axis=1, join='inner')
# print('res:\n', res)


# 主键拼接方式
# 获取df数据
# left = pd.read_excel('./主键拼接数据.xlsx', sheetname=0)
# right = pd.read_excel('./主键拼接数据.xlsx', sheetname=1)
# print('left:\n', left)
# print('right:\n', right)
# print('*' * 100)

# how='outer', on='key' 按照key列进行拼接,找的key 的并集
# res = pd.merge(left=left, right=right, how='outer', on='key')
# print('res:\n', res)

#  how='inner', on='key'按照key列进行拼接,找的key 的交集
# res = pd.merge(left=left, right=right, how='inner', on='key')
# print('res:\n', res)


# how='left', on='key' 按照左表中的key进行拼接,右表配合左表,如果右表没有的数据,用NaN来补齐
# res = pd.merge(left=left, right=right, how='left', on='key')
# print('res:\n', res)

#  how='right', on='key' 按照右表中的key进行拼接,左表配合右表,如果左表没有数据,用NaN来补齐
# res = pd.merge(left=left, right=right, how='right', on='key')
# print('res:\n', res)

# 当左右两个df中的列里面的值大部分相同,其列名不相同的场景
left = pd.read_excel('./主键拼接数据.xlsx', sheetname=2)
right = pd.read_excel('./主键拼接数据.xlsx', sheetname=3)
print('left:\n', left)
print('right:\n', right)
print('*' * 100)

# 主键拼接
# how='outer', left_on='kk', right_on='gg'
# 用左表中kk 与右表中的gg列的值进行拼接,并求取的是并集,如果没有的数据用NaN类补齐
# res = pd.merge(left=left, right=right, how='outer', left_on='kk', right_on='gg')
# print('res:\n', res)

#  how='inner', left_on='kk', right_on='gg'
# 用左表中kk 与右表中的gg列的值进行拼接,并求取的是交集
# res = pd.merge(left=left, right=right, how='inner', left_on='kk', right_on='gg')
# print('res:\n', res)

#  how='left', left_on='kk', right_on='gg'
# 用左表中kk 与右表中的gg列的值进行拼接,用右表来配合左表,如果右表中没有的值用NaN补齐
# res = pd.merge(left=left, right=right, how='left', left_on='kk', right_on='gg')
# print('res:\n', res)

#  how='right', left_on='kk', right_on='gg'
# 用左表中kk 与右表中的gg列的值进行拼接,用左表来配合右表,如果左表中没有的值用NaN来补齐
# res = pd.merge(left=left, right=right, how='right', left_on='kk', right_on='gg')
# print('res:\n', res)

02-数据合并案例

02-数据合并案例
import pandas as pd
import numpy as np

# 将detail 与info 与users拼接成一个大的数据集

# 加载detail
detail_0 = pd.read_excel("./meal_order_detail.xlsx", sheetname=0)
detail_1 = pd.read_excel("./meal_order_detail.xlsx", sheetname=1)
detail_2 = pd.read_excel("./meal_order_detail.xlsx", sheetname=2)
print('detail_0:\n', detail_0.shape)
print('detail_0:\n', detail_0.columns)
print('*' * 100)
print('detail_1:\n', detail_1.shape)
print('detail_1:\n', detail_1.columns)
print('*' * 100)
print('detail_2:\n', detail_2.shape)
print('detail_2:\n', detail_2.columns)
print('*' * 100)

# 将detail 拼接成一个完整的数据集
# 直接拼接
detail = pd.concat((detail_0, detail_1, detail_2), axis=0, join='inner')
print('detail:\n', detail.shape)
print('detail:\n', detail.columns)
print('*' * 100)

# 加载user 与info
info = pd.read_csv('./meal_order_info.csv', encoding='ansi')
print('info:\n', info.shape)
print('info:\n', info.columns)
print('*' * 100)
users = pd.read_excel('./users.xlsx')
print('users:\n', users.shape)
print('users:\n', users.columns)
print('*' * 100)

# 将user 与 info 进行主键拼接
res = pd.merge(left=users, right=info, how='inner', left_on='ACCOUNT', right_on='name')
print('res:\n', res.shape)
print('res:\n', res.columns)
print('*' * 100)

# 将detail 与 res 进行主键拼接
data = pd.merge(left=detail, right=res, how='inner', left_on='order_id', right_on='info_id')
print('data:\n', data.shape)
print('data:\n', data.columns)
print('*' * 100)

# 判断 emp_id_x  与 emp_id_y  是否都是一样的?
# a = np.all(data.loc[:,"emp_id_x"] == data.loc[:,"emp_id_y"])
# print('a:\n',a)

# 删除掉  emp_id_y info_id ACCOUNT
data.drop(labels=['emp_id_y', 'info_id', 'ACCOUNT'], axis=1, inplace=True)
print('最终的data:\n', data.shape)
print('最终的data:\n', data.columns)
print('*' * 100)

drop_list = []
# 剔除全部为空的列
for column in data.columns:
    # 判断为空的列
    res = data.loc[:, column].count()
    if res == 0:
        drop_list.append(column)

# 删除全部为空的列
data.drop(labels=drop_list, axis=1, inplace=True)
print('删除全部为缺失的列之后的结果:\n', data.shape)
print('删除全部为缺失的列之后的结果:\n', data.columns)
print('*' * 100)

drop_list = []
# 剔除 整列值都是一样的列
for column in data.columns:
    res = data.drop_duplicates(subset=column, inplace=False)
    if res.shape[0] == 1:
        drop_list.append(column)
print('*' * 100)
# 删除 值全部一样的列
data.drop(labels=drop_list, axis=1, inplace=True)
print('删除值全部一样的列之后的结果:\n', data.shape)
print('删除值全部一样的列之后的结果:\n', data.columns)
print('*' * 100)

发布了128 篇原创文章 · 获赞 24 · 访问量 4256

猜你喜欢

转载自blog.csdn.net/return_min/article/details/103953690