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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章