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
CodePudding user response:
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
CodePudding user response:
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
CodePudding user response:
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