实例:
import pandas as pd
# 读取 Excel 文件
df = pd.read_excel('your_file.xlsx')
# 假设要添加的数据
new_data = {'Column1': [value1, value2,...], 'Column2': [value3, value4,...],...}
# 创建新的 DataFrame
new_df = pd.DataFrame(new_data)
# 将新的数据添加到原 DataFrame 中
df = pd.concat([df, new_df], ignore_index=True) #连接后的结果会保留原来两个 DataFrame 的索引。
# 保存修改后的 Excel 文件
df.to_excel('your_file.xlsx', index=False)ignore_index =True
ignore_index=True 参数在 pd.concat() 函数中是可以不写的。
如果不写 ignore_index=True ,默认情况下,连接后的结果会保留原来两个 DataFrame 的索引。
如果写上 ignore_index=True ,则连接后的结果会重新生成从 0 开始的连续索引。
是否使用取决于您的具体需求,如果您不希望保留原有的索引,或者希望得到一个连续的新索引,就可以使用 ignore_index=True ;如果您希望保留原有的索引,就可以省略这个参数。