数据重构
首先导入numpy、pandas包和数据文件
import numpy as np import pandas as pd # 载入data文件中的:train-left-up.csv text = pd.read_csv(r'C:\Users\hp\hands-on-data-analysis-master\hands-on-data-analysis-master\第二章项目集合\data\train-left-up.csv')
1.数据合并
text_left_up = pd.read_csv("data/train-left-up.csv") text_left_down = pd.read_csv("data/train-left-down.csv") text_right_up = pd.read_csv("data/train-right-up.csv") text_right_down = pd.read_csv("data/train-right-down.csv")
合并方法一:用concat函数
# 将数据train-left-up.csv和train-right-up.csv横向合并为一张表,并保存这张表为result_up list_up = [text_left_up,text_right_up] result_up = pd.concat(list_up,axis=1) result_up.head() # 将train-left-down和train-right-down横向合并为一张表,并保存这张表为result_down。 list_down=[text_left_down,text_right_down] result_down = pd.concat(list_down,axis=1) # 将上边的result_up和result_down纵向合并为result。 result = pd.concat([result_up,result_down]) result.head()
#合并方法二:使用DataFrame自带的方法join方法和append
resul_up = text_left_up.join(text_right_up) result_down = text_left_down.join(text_right_down) result = result_up.append(result_down) result.head()
#合并方法三:使用Panads的merge方法和DataFrame的append方法
result_up = pd.merge(text_left_up,text_right_up,left_index=True,right_index=True) result_down = pd.merge(text_left_down,text_right_down,left_index=True,right_index=True) result = resul_up.append(result_down) result.head()