使用 Pandas - Python 将数据插入 Excel 文件
Inserting Data into an Excel file using Pandas - Python
我有一个 excel 文件,其中包含 60 个数据集的名称。
我正在尝试编写一段代码“进入”Excel 文件,访问特定数据集(其名称在 Excel 文件中),收集和分析一些数据最后,在 Excel 文件中创建一个新列并插入预先收集的信息。
我可以做大部分,除了添加新列和输入数据的部分。
我正在尝试做这样的事情:
path_data = **the path to the excel file**
recap = pd.read_excel(os.path.join(path_data,'My_Excel.xlsx')) # where I access the Excel file
recap['New information Column'] = Some Value
这样做正确吗?如果是这样,有人可以建议一个更好的方法吗(有效 ehehe)
非常感谢!
您可以使用 pandas 将 excel 文件导入 python。
import pandas as pd
df = pd.read_excel (r'Path\Filename.xlsx')
print (df)
如果你有很多张,那么你可以这样做:
import pandas as pd
df = pd.read_excel (r'Path\Filename.xlsx', sheet_name='sheetname')
print (df)
要添加新列,您可以执行以下操作:
df['name of the new column'] = 'things to add'
准备好后,您可以将其导出为 xlsx:
import openpyxl
# to excel
df.to_excel(r'Path\filename.xlsx')
我有一个 excel 文件,其中包含 60 个数据集的名称。
我正在尝试编写一段代码“进入”Excel 文件,访问特定数据集(其名称在 Excel 文件中),收集和分析一些数据最后,在 Excel 文件中创建一个新列并插入预先收集的信息。
我可以做大部分,除了添加新列和输入数据的部分。
我正在尝试做这样的事情:
path_data = **the path to the excel file**
recap = pd.read_excel(os.path.join(path_data,'My_Excel.xlsx')) # where I access the Excel file
recap['New information Column'] = Some Value
这样做正确吗?如果是这样,有人可以建议一个更好的方法吗(有效 ehehe)
非常感谢!
您可以使用 pandas 将 excel 文件导入 python。
import pandas as pd
df = pd.read_excel (r'Path\Filename.xlsx')
print (df)
如果你有很多张,那么你可以这样做:
import pandas as pd
df = pd.read_excel (r'Path\Filename.xlsx', sheet_name='sheetname')
print (df)
要添加新列,您可以执行以下操作:
df['name of the new column'] = 'things to add'
准备好后,您可以将其导出为 xlsx:
import openpyxl
# to excel
df.to_excel(r'Path\filename.xlsx')