How to preserve complicated excel header formats when manipulating data using Pandas Python?

Viewed 21

I am parsing a large excel data file to another one, however the headers are very abnormal. I tried to use "read_excel skiprows" and that did not work. I also tried to include the header in df = pd.read_excel(user_input, header= [1:3], sheet_name = 'PN Projection'), but then I get this error "ValueError: cannot join with no overlapping index names." To get around this I tried to name the columns by location and that did not work either.

When I run the code as shows below everything works fine, but past cell "U" I get the header titles to be "unnamed1, 2, ..." I understand this is because pandas is considering the first row to be the header(which are empty), but how do I fix this? Is there a way to preserve the headers without manually typing in the format for each cell? Any and all help is appreciated, thank you!

small section of the excel file header

the code I am trying to run

#!/usr/bin/env python 

import sys
import os
import pandas as pd

#load source excel file
user_input = input("Enter the path of your source excel file (omit 'C:'): ")     

#reads the source excel file
df = pd.read_excel(user_input, sheet_name = 'PN Projection') 

#Filtering dataframe
#Filters out rows with 'EOL' in column 'item status' and 'xcvr' in 'description'
df = df[~(df['Item Status'] == 'EOL')]
df = df[~(df['Description'].str.contains("XCVR", na=False))]
#Filters in rows with "XC" or "spartan" in 'description' column
df = df[(df['Description'].str.contains("XC", na=False) | df['Description'].str.contains("Spartan", na=False))]

print(df)

#Saving to a new spreadsheet called Filtered Data
df.to_excel('filtered_data.xlsx', sheet_name='filtered_data')
1 Answers

If you do not need the top 2 rows, then:

df = pd.read_excel(user_input, sheet_name = 'PN Projection',error_bad_lines=False, skiprows=range(0,2)

This has worked for me when handling several strangely formatted files. Let me know if this isn't what your looking for, or if their are additional issues.

Related