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?
            Asked
            
        
        
            Active
            
        
            Viewed 2,868 times
        
    3
            
            
        - 
                    1list comprehension: `['s' + str(x) for x in range(1,21)]` you can then either pass this direct to `DataFrame` ctor code or override the columns: `df.columns = column_list` – EdChum Jan 06 '17 at 10:39
- 
                    You mean column list, `['s%s' % x for x in range(1, 21)]`? – Zero Jan 06 '17 at 10:39
1 Answers
6
            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')
 
     
    