DAY 6
0
AI & Data

list

``````# 合併兩個list
list1 = [1,2,3]
list2 = [4,5,6]
list3 = list1 + list2
list3
# [1, 2, 3, 4, 5, 6]
``````

dict

``````# 合併兩個dict
dict1 = {"a":1, "b":2, "c":3}
dict2 = {"e":4, "f":5}
dict3 = dict(dict1) # 先複製一份過去
dict3.update(dict2) # 剩下的用update的
dict3
# {'a': 1, 'b': 2, 'c': 3, 'e': 4, 'f': 5}
``````

set

``````# 合併兩個set
set1 = {1,2,3}
set2 = {4,5,6}
set3 = set1 | set2 # 用or來合併
set3
# {1, 2, 3, 4, 5, 6}
``````

numpy.array

``````# 合併兩個numpy向量
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
``````

`axis=0`按照最外層元素順序

``````# 按照最外層元素順序
np.concatenate((a, b), axis=0)
# array([[1, 2],
#        [3, 4],
#        [5, 6]])
``````

`a`為例子，其最外層的順序應該是`[1,2]`然後`[3,4]`

`axis=1`按第1層裡面元素順序

``````# 按第1層裡面元素順序
np.concatenate((a, b.T), axis=1) # b有轉置
# array([[1, 2, 5],
#        [3, 4, 6]])
``````

`axis=None`全部攤平

``````# 全部攤平
np.concatenate((a, b), axis=None)
# array([1, 2, 3, 4, 5, 6])
``````

pandas.DataFrame

``````import pandas as pd
df1 = pd.DataFrame({"a":[1,2], "b":[3,4]})
df2 = pd.DataFrame({"a":[10], "b":[20]})
``````

df1

df2

一般來說按照`axis=0`(可省略)合併

``````pd.concat([df1,df2],axis=0) # aixs=0可以省略
``````

`axis=1`的效果

``````pd.concat([df1,df2],axis=1)
``````