venkatesh addanki
venkatesh addanki

Reputation: 13

Length mismatch while applying a logic to Dataframe

I'm trying to change to uppercase of alternate column names of a Dataframe having 6 columns.

input :

df.columns[::2].str.upper()

Output :

Index(['FIRST_NAME', 'AGE_VALUE', 'MOB_#'], dtype='object')

Now i want to apply this to Dataframe.

input : df.columns= df.columns[::2].str.upper()

ValueError: Length mismatch: Expected axis has 6 elements, new values have 3 elements

Upvotes: 1

Views: 84

Answers (1)

Dishin H Goyani
Dishin H Goyani

Reputation: 7723

You can use rename

df
   a  b  c  d  e  f
0  a  b  c  d  e  f
column_names = dict(zip(df.columns[::2], df.columns[::2].str.upper()))
column_names
{'a': 'A', 'c': 'C', 'e': 'E'}

df = df.rename(columns=column_names)
df
   A  b  C  d  E  f
0  a  b  c  d  e  f

Upvotes: 0

Related Questions