0
我正在建立一個進程,以「外部連接」兩個csv文件並將結果導出爲json對象。現在Python - 熊貓 - 如何從數據幀合併後從to_json刪除空值
# read the source csv files
firstcsv = pandas.read_csv('file1.csv', names = ['main_index','attr_one','attr_two'])
secondcsv = pandas.read_csv('file2.csv', names = ['main_index','attr_three','attr_four'])
# merge them
output = firstcsv.merge(secondcsv, on='main_index', how='outer')
jsonresult = output.to_json(orient='records')
print(jsonresult)
,這兩個CSV文件中是這樣的:
file1.csv:
1, aurelion, sol
2, lee, sin
3, cute, teemo
file2.csv:
1, midlane, mage
2, jungler, melee
而且我想生成的JSON喜歡輸出:
[{"main_index":1,"attr_one":"aurelion","attr_two":"sol","attr_three":"midlane","attr_four":"mage"},
{"main_index":2,"attr_one":"lee","attr_two":"sin","attr_three":"jungler","attr_four":"melee"},
{"main_index":3,"attr_one":"cute","attr_two":"teemo"}]
相反,我越來越對我line with main_index = 3
{"main_index":3,"attr_one":"cute","attr_two":"teemo","attr_three":null,"attr_four":null}]
在輸出中自動添加空值。 我想刪除他們 - 我環顧四周,但我找不到一個正確的方法來做到這一點。
希望有人能幫助我!
這個工作,但我失去的元素(比方說,我指定用reindex_axis方法的按訂單生產) 我想我需要使用的一些OrderedDict的順序排序,以保持排序... – Mik1893
更新,以保持訂單 – Hazzles
我只是找到它昨天唉,晚上......但非常感謝幫助! – Mik1893