0

How can I create dataframe columns automatically with different names

I have this code

df=pd.DataFrame(columns=['A']*5)
df.loc[len(df)]=[1,2,3,4,5]

Which gives the result

   A  A  A  A  A
0  1  2  3  4  5

And I want the column names to be

   A1  A2  A3  A4  A5
0  1   2   3   4   5

3 Answers 3

4

If possible pass to DataFrame correct columns names:

df=pd.DataFrame(columns=[f'A{i}' for i in range(1, 6)])
df.loc[len(df)]=[1,2,3,4,5]
print (df)
  A1 A2 A3 A4 A5
0  1  2  3  4  5

If need change values later with enumerate all columns names use:

df=pd.DataFrame(columns=['A']*5)

s = df.columns.to_series()
df.columns += s.groupby(s).cumcount().add(1).astype(str)
df.loc[len(df)]=[1,2,3,4,5]

print (df)
  A1 A2 A3 A4 A5
0  1  2  3  4  5

df=pd.DataFrame(columns=['A']*2 + ['B'] * 2 + ['C'])

s = df.columns.to_series()

df.columns += s.groupby(s).cumcount().add(1).astype(str)

df.loc[len(df)]=[1,2,3,4,5]

print (df)
  A1 A2 B1 B2 C1
0  1  2  3  4  5
Sign up to request clarification or add additional context in comments.

Comments

2

One option is with enumerate, if you are doing this after creating the DataFrame (doing this when creating the DataFrame, as suggested by @jezrael, IMO is a better option):

df.columns = [f"{col}{num+1}" for num, col in enumerate(df.columns)]
df

   A1  A2  A3  A4  A5
0   1   2   3   4   5

2 Comments

Oops I just had added this alternative :p
all good; the more the merrier :)
2

To handle an arbitrary input list use enumerate:

df=pd.DataFrame(columns=[f'{a}{b}' for b,a in enumerate(['A']*5, start=1)])
df.loc[len(df)]=[1,2,3,4,5]

To handle any type of object without creating a copy, use itertools.repeat:

from itertools import repeat
df=pd.DataFrame(columns=[f'{a}{b+1}' for a,b in zip(repeat('A'), range(5))])
df.loc[len(df)]=[1,2,3,4,5]

output:

   A1  A2  A3  A4  A5
0   1   2   3   4   5

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.