用Python编写excel文件

时间:2017-06-01 19:14:08

标签: python excel

我几天前发布了这个问题的一部分并得到了一个很好的答案,但这解决了我的部分问题。 所以,我有一个excel文件,需要在其上进行一些数据挖掘,之后需要输出另一个格式相同的excel文件.xlsx 问题是我写完文件后得到一个奇怪的列,在使用Anaconda编写之前无法看到。这使得制定战略以应对其外观变得更加困难。最初我虽然通过将宽度减小到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')

问题的实际观点: enter image description here

任何有建设性的想法都值得赞赏。谢谢!

1 个答案:

答案 0 :(得分:4)

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