1

I am trying to open my Excel sheet on pandas. I've found some interesting online tutorial. However, I'm stuck trying to organize my header.

So far, I've following the code:

import pandas as pd
pd.set_option('display.width', 300) 
fuel_file = (r'C:\Users\Sarah\Desktop\test\test2.xlsx')
xl = pd.ExcelFile(fuel_file)
print(xl.sheet_names)
fuel_df = xl.parse("Moon", header=[1], index_col=[0,1,2])
print(fuel_df.head())
print(fuel_df.columns)

weeknumbers_list = list(fuel_df.columns)
spacecustomers = fuel_df.iloc[1]

start_position = 0

for weeknr_pos in positions_weeknumbers:
    for pos in range(start_position, weeknr_pos):
        weeknumbers_list[pos] = weeknumbers_list[weeknr_pos]
    start_position = weeknr_pos + 1
    weeknumbers_list[weeknr_pos] = "Remove"

fuel_df.columns = weeknumbers_list
fuel_df.drop("Remove", axis=1, inplace=True)

The problem is in :

for weeknr_pos in positions_weeknumbers:

The error message:

**for weeknr_pos in positions_weeknumbers: TypeError: 'int' object is not iterable**

I don't know how to define positions_weeknumbers.

1 Answer 1

1

You are getting the error "'int' object is not iterable" because "positions_weeknumbers" will be having an int value and you can't iterate an int object.You have to specify int object inside range() function as follows:

import pandas as pd
pd.set_option('display.width', 300) 
fuel_file = (r'C:\Users\Sarah\Desktop\test\test2.xlsx')
xl = pd.ExcelFile(fuel_file)
print(xl.sheet_names)
fuel_df = xl.parse("Moon", header=[1], index_col=[0,1,2])
print(fuel_df.head())
print(fuel_df.columns)

weeknumbers_list = list(fuel_df.columns)
spacecustomers = fuel_df.iloc[1]

start_position = 0

for weeknr_pos in range(positions_weeknumbers):
    for pos in range(start_position, weeknr_pos):
        weeknumbers_list[pos] = weeknumbers_list[weeknr_pos]
    start_position = weeknr_pos + 1
    weeknumbers_list[weeknr_pos] = "Remove"

fuel_df.columns = weeknumbers_list
fuel_df.drop("Remove", axis=1, inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.