lst1 = [
[“X”,”Y”],
[“Z”,”W”]
]
lst2 = [
[“a”,”b”],
[“c”,”d”]
]
import pandas as pd
df1 = pd.DataFrame(lst1) ;
print(“DataFrame1:\n”,df1)
df2 = pd.DataFrame(lst2) ;
print(“DataFrame2:\n”,df2)
df3 = pd.concat( [df1,df2] ) ;
print(“DataFrame3:\n”,df3)
df4 = pd.concat( [df1,df2],axis=1 ) ;
print(“DataFrame4:\n”,df4)
#[df1,df2] 要用[]包覆,
#換句話說,
#輸入一個list,其元素為DF or Series
df5 = pd.concat( [df1,df2],axis=1 ,
ignore_index=True) ;
print(“DataFrame5:\n”,df5)
#ignore_index = True
#可以忽略合併時舊的 index 欄位,
#改採用自動產生的 index
輸出結果:
append:
對比list.append(),改變原list
DataFrame.append()產生新的DataFrame
append:
insert:
insert
雖然最後一欄的欄名稱是8
但語法loc=3
若用loc=4:
loc超過4的話,則會觸發
IndexError: index 5 is out of bounds for axis 0 with size 4