用Python编写excel文件

几天前我发布了这个问题的一部分,有一个很好的答案,但这只是解决了我的问题的一部分。 所以,我有一个Excel文件需要做一些数据挖掘,然后需要出来另一个Excel格式相同的格式.xlsx问题是,我得到一个奇怪的列后,我写的文件,这是不能在使用Anaconda写作之前看过。 这使得制定一个反制它外观的策略变得更加困难。 最初我虽然我通过将宽度减less到0来解决问题,但显然在某些时候文件需要在文本中进行转换,然后列再次出现。 更多的细节在这里是我的代码的一部分:

import os import pandas as pd import numpy as np import xlsxwriter # Retrieve current working directory (`cwd`) cwd = os.getcwd() cwd # Change directory os.chdir("/Users/s7c/Documents/partsstop") # Assign spreadsheet filename to `file` file = 'file = 'SC daily inventory retrieval columns for reports'.xlsx # Load spreadsheet xl = pd.ExcelFile(file) # Load a sheet into a DataFrame by name: df df = xl.parse('Sheet1') #second file code: #select just the columns we need and rename them: df2 = df.iloc[:, [1, 3, 6, 9]] df2.columns = ['Manufacturer Code', 'Part Number', 'Qty Available', 'List Price'] #then select just the rows we need: df21 = df2[df2['Manufacturer Code'].str.contains("DRP")]#13837 entries #select just the DRP, first 3 characters and dropping the ones after: df21['Manufacturer Code'] = df21['Manufacturer Code'].str[:3] #add a new column: #in order to do that we need to convert the next column to numeric: df21['List Price'] = pd.to_numeric(df21['List Price'], errors='coerce') df21['Dealer Price'] = df21['List Price'].apply(lambda x: x*0.48) #new column equals half of other column writer = pd.ExcelWriter('example2.xlsx', engine='xlsxwriter') # Write your DataFrames to a file df21.to_excel(writer, 'Sheet1') 

问题的实际观点: 在这里输入图像描述

任何build设性的想法是赞赏。 谢谢!

此列似乎是您的DataFrame的索引。 您可以通过将index=False传递给to_excel()来排除它。