Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change column name pandas

Tags:

python

pandas

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].
like image 762
fred.schwartz Avatar asked Dec 04 '18 14:12

fred.schwartz


People also ask

How do you rename a column in Python?

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.

How do you rename columns?

Select a column, and then select Transform > Rename. You can also double-click the column header. Enter the new name.


2 Answers

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
like image 176
jezrael Avatar answered Oct 23 '22 21:10

jezrael


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.

like image 32
piRSquared Avatar answered Oct 23 '22 23:10

piRSquared