Task3 資料重構 學習筆記

2021-10-23 17:42:44 字數 3849 閱讀 1320

2、資料運用

方法一:concat()

concat(objs, axis=

0, join=

'outer'

, join_axes=

none

, ignore_index=

false

, keys=

none

, levels=

none

, names=

none

, verify_integrity=

false

, copy=

true

):

引數介紹:

objs:需要連線的物件集合,一般是列表或字典;

axis:連線軸向;

join:引數為『outer』或『inner』;

keys=:建立層次化索引;

ignore_index=true:重建索引

concat方法相當於資料庫中的全連線(union all),它不僅可以指定連線的方式(outer join或inner join)還可以指定按照某個軸進行連線。與資料庫不同的是,它不會去重,但是可以使用drop_duplicates方法達到去重的效果。

import pandas as pd

import numpy as np

train=pd.read_csv(

'titanic/train.csv'

)train_left_up=pd.read_csv(

'titanic/data/train_left_up.csv'

)train_left_down=pd.read_csv(

'titanic/data/train_left_down.csv'

)train_right_up=pd.read_csv(

'titanic/data/train_right_up.csv'

)train_right_down=pd.read_csv(

'titanic/data/train_right_down.csv'

)#方法一:concat()

#axis=1,表示跨行進行合併,也就是將量表左右相連,如果是axis=0,就是將量表上下相連

a_result_up=pd.concat(

[train_left_up,train_right_up]

,axis=1)

#左右相連

b_result_down=pd.concat(

[train_left_down,train_right_down]

,axis=1)

#左右相連

c_result=pd.concat(

[a_result_up,b_result_down]

,axis=0)

#上下相連(預設是上下相連)

c_result

join(self, other, on=

none

, how=

'left'

, lsuffix=

'', rsuffix=

'',sort=

false

):

d_result_up=train_left_up.join(train_right_up)

#使用join左右相連

e_result_down=train_left_down.join(train_right_down)

#使用join左右相連

# f_result

方法三:

merge(left, right, how=

'inner'

, on=

none

, left_on=

none

, right_on=

none

, left_index=

false

, right_index=

false

, sort=

true

, suffixes=

('_x'

,'_y'

), copy=

true

, indicator=

false

)

引數介紹:

left和right:兩個不同的dataframe;

how:連線方式,有inner、left、right、outer,預設為inner;

on:指的是用於連線的列索引名稱,必須存在於左右兩個dataframe中,如果沒有指定且其他引數也沒有指定,則以兩個dataframe列名交集作為連線鍵;

left_on:左側dataframe中用於連線鍵的列名,這個引數左右列名不同但代表的含義相同時非常的有用;

right_on:右側dataframe中用於連線鍵的列名;

left_index:使用左側dataframe中的行索引作為連線鍵;

right_index:使用右側dataframe中的行索引作為連線鍵;

sort:預設為true,將合併的資料進行排序,設定為false可以提高效能;

suffixes:字串值組成的元組,用於指定當左右dataframe存在相同列名時在列名後面附加的字尾名稱,預設為(』_x』, 『_y』);

copy:預設為true,總是將資料複製到資料結構中,設定為false可以提高效能;

indicator:顯示合併資料中資料的**情況

g_result_up=pd.merge(train_left_up,train_right_up,left_index=

true

,right_index=

true

)h_result_down=pd.merge(train_left_down,train_right_down,left_index=

true

,right_index=

true

)i_result

在用pandas進行資料重排時,經常用到stack和unstack兩個函式。stack的意思是堆疊,堆積,unstack即「不要堆疊」

常見的資料的層次化結構有兩種,一種是**,一種是「花括號」

**在行列方向上均有索引(類似於dataframe),花括號結構只有「列方向」上的索引(類似於層次化的series),結構更加偏向於堆疊(series-stack,方便記憶)。stack函式會將資料從」**結構「變成」花括號結構「,即將其行索引變成列索引,反之,unstack函式將資料從」花括號結構「變成」**結構「,即要將其中一層的列索引變成行索引。

**stack函式將原**的每行變成一列來儲存

import pandas as pd

import numpy as np

result=pd.read_csv(

'titanic/data/result.csv'

)unit_result=result.stack(

)unit_result

計算不同性別的平均票價

動手學資料分析 task3 資料重構

一 資料合併 1 使用pd.concat objs,axis 0,join outer list up text left up,text right up result up pd.concat list up,axis 1 result up.head resul up text left up...

python學習打卡 Task3

集合條件語句 迴圈語句 鍵必須不可變,所以可以用數字,字串或元組作為鍵,而列表不行 dic print dic name 執行結果 pythondic age 100 更改age的值 dic date 20190514 增加date print dic 執行結果 del dic name 刪除nam...

TASK3 資料型別

magic 函式用來產生魔方矩陣。魔方矩陣中每行 列和兩條對角線上的元素和相等。a magic 3 a 816 3574 92 b magic 4 b 1623 135111089 76124 1415 1主要包括數值型別,邏輯型別,字串,函式控制代碼,結構體和單元陣列型別。整數型別 數值的預設儲存...