我连接了437个表,得到了3列状态,因为我的同事每天都想给它一个不同的名称,("状态","状态:"和"状态"),有没有办法将这3列连接到一个名为"状态"的列?*also my code uses append, I just saw its deprecated, will it work the same using concat? any way to make it give the same results as append?.
我试过:
excl_merged.rename(columns={"state:": "state", "State": "state"})
但它什么也做不了。
我使用的代码:
# importing the required modules
import glob
import pandas as pd
# specifying the path to csv files
path = "X:/.../Admission_merge"
# csv files in the path
file_list = glob.glob(path + "/*.xlsx")
# list of excel files we want to merge.
# pd.read_excel(file_path) reads the excel
# data into pandas dataframe.
excl_list = []
for file in file_list:
excl_list.append(pd.read_excel(file)) #use .concat will it give the columns in the same order?
# create a new dataframe to store the
# merged excel file.
excl_merged = pd.DataFrame()
for excl_file in excl_list:
# appends the data into the excl_merged
# dataframe.
excl_merged = excl_merged.append(
excl_file, ignore_index=True)
# exports the dataframe into excel file with
# specified name.
excl_merged.to_excel('X:/.../Admission_MERGED/total_admission_2021-2023.xlsx', index=False)
print("Merge finished")
有什么建议,我可以如何改进它?也有没有办法删除未命名的空列?
多谢了。
1条答案
按热度按时间p5cysglq1#
您可以使用
pd.concat
:file1.xlsx:
file2.xlsx:
file3.xlsx:
如果有空列,可以在追加到数据列表之前使用
data.append(df.dropna(how='all', axis=1))
。