
在一個文件中,我有兩張表,它們共用一些列。我需要根據列名稱合併工作表,如果該列不存在,請新增它。 IE。我有
表1
+----+-------+--------------+
| id | name | description |
+----+-------+--------------+
| 1 | name1 | description1 |
| 2 | name2 | description2 |
+----+-------+--------------+
表2
+----+-------+--------------+--------+
| id | name | description | title |
+----+-------+--------------+--------+
| 3 | name3 | description3 | title3 |
| 4 | name4 | description4 | title4 |
+----+-------+--------------+--------+
所需輸出
+----+-------+--------------+--------+
| id | name | description | title |
+----+-------+--------------+--------+
| 1 | name1 | description1 | |
| 2 | name2 | description2 | |
| 3 | name3 | description3 | title3 |
| 4 | name4 | description4 | title4 |
+----+-------+--------------+--------+
有什麼辦法可以做到這一點嗎?
答案1
你可以這樣做 python + pandas
import pandas as pd
import numpy as np
import glob
a = glob.glob("C:/Documents and Settings/Administrator/My Documents/*.xlsx")
all_data = pd.DataFrame()
for f in a:
df = pd.read_excel(f)
all_data = all_data.append(df,ignore_index=True,sort=False)
print all_data
這將合併您指定位置的所有數據,並將顯示