Adam Jermann
Adam Jermann

Reputation: 106

Generating column names with string and number

Very basic question: what is the easiest way (least code) to generate a set of column names for a pandas dataframe when I would like to get 20 columns with names: s1, s2, s3, ... ,s20?

Upvotes: 3

Views: 3199

Answers (1)

EdChum
EdChum

Reputation: 394279

You can use a list comprehension to generate the column names:

In [66]:
col_list = ['s' + str(x) for x in range(1,21)]
col_list

Out[66]:
['s1',
 's2',
 's3',
 's4',
 's5',
 's6',
 's7',
 's8',
 's9',
 's10',
 's11',
 's12',
 's13',
 's14',
 's15',
 's16',
 's17',
 's18',
 's19',
 's20']

After which you can either pass this as the column arg in DataFrame ctor:

In [70]:
df = pd.DataFrame(np.random.randn(5,20), columns=col_list)
df.columns

Out[70]:
Index(['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
       's12', 's13', 's14', 's15', 's16', 's17', 's18', 's19', 's20'],
      dtype='object')

Or just overwrite the columns attribute by assigning directly:

In [71]:
df = pd.DataFrame(np.random.randn(5,20))
df.columns = col_list
df.columns

Out[71]:
Index(['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
       's12', 's13', 's14', 's15', 's16', 's17', 's18', 's19', 's20'],
      dtype='object')

You can also use rename or rename_axis but they're for overwriting pre-existing column names for which there is already a related post

You can also add a prefix to a Series created from a range:

In [76]:
col_list = 's' + pd.Series(np.arange(1,21)).astype(str)
df.columns= col_list
df.columns

Out[76]:
Index(['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11',
       's12', 's13', 's14', 's15', 's16', 's17', 's18', 's19', 's20'],
      dtype='object')

Upvotes: 6

Related Questions