Home > Net >  Renaming columns in pandas dynamically in python
Renaming columns in pandas dynamically in python

Time:03-29

I have a file(say temp.rule) which looks like below:

11,12,13,14,0,0.55
27,28,29,30,1,0.67
31,32,33,34,1,0.84
75,76,77,78,3,0.51
51,52,53,54,2,0.28
55,56,57,58,2,0.77
59,60,61,62,2,0.39
35,36,37,38,1,0.45
39,40,41,42,1,0.82
43,44,45,46,1,0.92
63,64,65,66,2,0.41
90,91,92,93,3,0.97

The 1st 4 columns are attribute columns and the last 2 rows are class and fitness. I tried to read this as:

df = pd.read_csv('temp.rule', names=['att1','att2','att3','att4','class','fitness'])

But this can have multiple column values for attributes, that is there can be a file say temp_1.rule which can have 6 attributes. The last 2 columns will always be class and fitness. How to make that thing dynamic instead of names=['att1','att2','att3','att4'...]

CodePudding user response:

You can create numeric columns and then rename by dict:

df = pd.read_csv('temp.rule', header=None)
d = {**{x: f'att{x 1}' for x in df.columns[:-2]}, 
     **dict(zip(df.columns[-2:], ['class','fitness']))}

df = df.rename(columns=d)
print (df)
    att1  att2  att3  att4  class  fitness
0     11    12    13    14      0     0.55
1     27    28    29    30      1     0.67
2     31    32    33    34      1     0.84
3     75    76    77    78      3     0.51
4     51    52    53    54      2     0.28
5     55    56    57    58      2     0.77
6     59    60    61    62      2     0.39
7     35    36    37    38      1     0.45
8     39    40    41    42      1     0.82
9     43    44    45    46      1     0.92
10    63    64    65    66      2     0.41
11    90    91    92    93      3     0.97

If there are always at least 2 columns:

df.columns = [f'att{x 1}' for x in df.columns[:-2]]   ['class','fitness']

CodePudding user response:

I'd use shape to get the column number and construct the columns with a list comprehension:

df = pd.read_csv('temp.rule')
df.columns = [f"att{i 1}" for i in range(df.shape[1] - 2)]   ["class", "fitness"]

  • Related