我是用Python來寫程式的,然後有使用到pandas這個模組,我原本想用pandas裡的函數merge來做資料的"聯集"和"交集",結果做完後卻變成第二張圖這樣
請問大家有什麼方式可以解決呢?
使用中文print,確實會造成排版錯亂,直接顯示dataframe變數,Jupyter 會轉成 html table顯示,故排列會整齊,也可以使用下列指令:
from IPython.display import display
display(df1)
因為我看你的code個人覺得沒什麼問題,
理論上來說應該會是排版整齊的東西。
我會嘗識的作法有兩項
import pandas as pd
data1 = pd.DataFrame({"longlongKey":["A1","A2","A3","A4"],
" sK ":["BBBBBB1 ","BBBBBB2","BBBBBB3","BBBBBB4"],
" midKey ":["CC1","CC2","CC3","CC4"]})
data2 = pd.DataFrame({"longlongKey":["A1","A2","A3","A4"],
" LKK ":["LLLL1","L2","LLLLLLLLLLLLL3","LLLLL4"],
" NMS":["NMNMNMNMN1","1"," 2 ","3"]})
r = pd.merge(data1,data2,how="outer")
print(r)
2. 或許是編譯器排版跑掉,嘗試用其他編譯器試試看。我個人沒用過jupyter,所以我會想會不會是jupyter的問題
以上僅供參考,我個人沒遇過這樣的狀況,也很好奇到底是什麼原因,若有找到答案可以煩請告知我一聲感謝~