Is it possible to change the name of a column in a pandas data frame if it starts with a certain word.
e.e If column starts with DEP then change the full name to KEEP.
[col for col in df if col.startswith('DEP') then KEEP].
You can rename the columns using the rename() method by using the axis keyword in it. In this method, you'll specify the columns as Python Set within { } rather specifying columns as a Python Dictionary with Key-Value Pairs.
Select a column, and then select Transform > Rename. You can also double-click the column header. Enter the new name.
Is it possible, but not recommended, because get duplicated columns names:
df = pd.DataFrame({
'DEP1':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'DEP2':list('aaabbb')
})
print (df)
DEP1 B C DEP2
0 a 4 7 a
1 b 5 8 a
2 c 4 9 a
3 d 5 4 b
4 e 5 2 b
5 f 4 3 b
df.columns = ['KEEP' if col.startswith('DEP') else col for col in df]
print (df)
KEEP B C KEEP
0 a 4 7 a
1 b 5 8 a
2 c 4 9 a
3 d 5 4 b
4 e 5 2 b
5 f 4 3 b
So then if select column KEEP
it return all duplicated columns in DataFrame:
print (df['KEEP'])
KEEP KEEP
0 a a
1 b a
2 c a
3 d b
4 e b
5 f b
So if want filter all columns starting with DEP
use filter
with regex ^
for starting string:
df1 = df.filter(regex='^DEP')
#alternative solution
#df1 = df.loc[:, df.columns.str.startswith('DEP')]
print (df1)
DEP1 DEP2
0 a a
1 b a
2 c a
3 d b
4 e b
5 f b
You can pass a callable to rename
df.rename(columns=lambda x: 'KEEP' if x.startswith('DEP') else x)
You can either reassign this to the name df
or use the inplace=True
argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With