DataWhale 动手学数据分析 Task03 数据重构

2021/6/19 23:30:14

本文主要是介绍DataWhale 动手学数据分析 Task03 数据重构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# 导入基本库
import numpy as np
import pandas as pd

任务一:读取数据,并观察数据之间的关系

train_left_up=pd.read_csv('train-left-up.csv')
train_left_down=pd.read_csv(r'.\train-left-down.csv')
train_right_up=pd.read_csv(r'.\train-right-up.csv')
train_right_down=pd.read_csv(r'.\train-right-down.csv')

上面四组数据即为train.csv数据集分成的四块

任务二:使用concat方法:将数据train-left-up.csv和train-right-up.csv横向合并为一张表,并保存这张表为result_up

result_up=pd.concat([train_left_up,train_right_up],axis=1)
result_up.head()

 任务三:使用concat方法:将train-left-down和train-right-down横向合并为一张表,并保存这张表为result_down。然后将上边的result_up和result_down纵向合并为result。

result_down=pd.concat([train_left_down,train_right_down],axis=1)
result=pd.concat([result_up,result_down],axis=0)
result.head()
result.shape

任务四:使用DataFrame自带的方法join方法和append:完成任务二和任务三的任务 

 

##### 当一个DataFrame是一个查找表, 其中包含添加到另一个DataFrame中的其他数据时, join()方法通常很有用。这是一种方便的方法, 可以将两个索引不同的DataFrame的列合并为一个DataFrame。
* (1)识别联接键  为了确定适当的连接键, 首先, 我们必须定义在DataFrame之间共享的必填字段。这两个DataFrame都由具有相同名称并且包含相同数据的列组成
* (2)内部联接  内部联接可以定义为最常用的联接。基本上, 其主要任务是基于连接键将两个DataFrame组合在一起并返回一个新的DataFrame。返回的DataFrame 仅 包含在两个原始DataFrame中都具有匹配值的选定行。
* (3)左联接  如果我们想在不丢失任何数据的情况下向DataFrame中添加一些信息, 我们可以简单地通过称为”左外部连接”或”左连接”的另一种类型的连接来做到这一点。   像内部联接一样, 左联接也使用联接键来组合两个DataFrame, 但是与内部联接不同, 它会返回左侧DataFrame的所有行, 即使那些联接键不包含右侧DataFrame中值的行也是如此 

DataFrame.join(other, on=None, how='left', lsuffix='', rsuffix='', sort=False)
other:DataFrame, Series, 或 list of DataFrame索引应与此列中的一列相似。如果传递了Series,则必须设置其name属性,并将其用作结果联接的DataFrame中的列名称。

on:str, list of str, 或 array-like, 可选参数调用方中要加入索引的列或索引级别名称other,否则加入index-on-index。如果给出多个值,则otherDataFrame必须具有MultiIndex。如果调用DataFrame中尚未包含数组,则可以将其作为连接键传递。就像Excel的VLOOKUP操作一样。

how:{‘left’, ‘right’, ‘outer’, ‘inner’}, 默认为 ‘left’如何处理两个对象的操作。

左:使用调用框架的索引(如果指定了列,则为列)

右:使用other的索引。

external:与调用框架的索引(如果指定为on,则为列)的形式联合other的索引,然后对其进行排序。从字典上看。

inner:与调用框架的索引(或列,如果指定为on)形成交集,other的索引,保留调用顺序。

lsuffix:str, 默认为 ‘’在左框架的重叠列中使用的后缀。

rsuffix:str, 默认为 ‘’在右框架的重叠列中使用的后缀。

sort:bool, 默认为 False通过联接关键字按字典顺序对结果DataFrame进行排序。如果为False,则联接键的顺序取决于联接类型(how关键字)。

result_up=train_left_up.join(train_right_up)
result_down=train_left_down.join(train_right_down)
result_up.append(result_down)

  任务五:使用Panads的merge方法和DataFrame的append方法:完成任务二和任务三的任务

result_up=pd.merge(train_left_up,train_right_up,left_index=True,right_index=True)
result_down=pd.merge(train_left_down,train_right_down,left_index=True,right_index=True)
result=result_up.append(result_down)

【思考】对比merge、join以及concat的方法的不同以及相同。思考一下在任务四和任务五的情况下,为什么都要求使用DataFrame的append方法,如何只要求使用merge或者join可不可以完成任务四和任务五呢?
* 因为merge和join都是做列向的合并,append是行向的合并,因此要上下合并都需要append;只用merge和join不能够完成任务四和五 

 

2.5.1 任务一:将我们的数据变为Series类型的数据


* DataFrame是二维数据表,Series是一维数组其中列名作为索引名。 DF转换成Series相当于二维转换成一维
* stack() 函数是将层级最低(默认)的column转化为index
* unstack()默认是将排位最靠后的index转成column(column放到下面) unstack('1') 

* Pandas中的merge、concat、join等详解:https://www.cnblogs.com/dataxon/p/12634897.html

text=pd.read_csv('result.csv')
text_ser=text.stack()
text_ser.to_csv('text_ser.csv')
text_ser2=pd.read_csv('text_ser.csv')
text_ser2.columns=['编号','信息字段','个人信息']
text_ser2.head()

 

 

 

 



这篇关于DataWhale 动手学数据分析 Task03 数据重构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程