I have a df in which I need to rename 40 column names to empty string. this can be achieved by using .rename()
, but I need to provide all the column names in dict, which needs to be renamed. I was searching for some better way to rename columns by some pattern matching. wherever it finds NULL/UNNAMED in column name, replace that with empty string.
df1: original df (In actual df, i have around 20 columns as NULL1-NULL20 and 20 columns as UNNAMED1-UNNAMED20)
NULL1 NULL2 C1 C2 UNNAMED1 UNNAMED2
0 1 11 21 31 41 51
1 2 22 22 32 42 52
2 3 33 23 33 43 53
3 4 44 24 34 44 54
desired output df:
C1 C2
0 1 11 21 31 41 51
1 2 22 22 32 42 52
2 3 33 23 33 43 53
3 4 44 24 34 44 54
This can be achieved by
df.rename(columns={'NULL1':'', 'NULL2':'', 'UNNAMED1':'', 'UNNAMED2':''}, inplace=True)
But I dont want to create the long dictionary of 40 elements
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…