dubbbdan
dubbbdan

Reputation: 2740

Pandas adding `.1` suffix to column names

I have an excel workbook with identically structured tables on the same sheet. I have made a loop to read them in:

for usecols in['A:L','N:Y']:
    df = pd.read_excel(r"Frequency_CI_extracted.xlsx", 
                       usecols=usecols, skiprows=3, 
                       sheet_name = 'Quantiles_CI_inches')

The resulting data frame for the first iteration (i.e. usecols="A:L") returns a data frame with the headers in the excel workbook. When the loop is on its second iteration, the resulting data frame has a .1 suffix one each column name.

enter image description here Can anyone explain this behavior?

pd.__version__
Out[9]: '1.0.3'

Upvotes: 4

Views: 2068

Answers (2)

prt13
prt13

Reputation: 11

This happened in my code when I had a capitalized version of the column name being added after the lower case version, then forced ALL column names to lowercase.

df.columns = ['this', 'that', 'thethird']
df['theThird'] = 99
df.columns = df.columns.str.lower()

df.columns

gives

['this', 'that', 'thethird', 'thethird.1']

check that there isn't some sort of column name adjustment like this happening in another function or being called between reading column names

Upvotes: 1

roshua
roshua

Reputation: 36

This happens when you have two identical column names in a dataframe. That's why the second iteration is adding .1

Upvotes: 2

Related Questions