Remove last two characters from column names of all the columns in Dataframe - Pandas

Observer

I am joining the two dataframes (a,b) with identical columns / column names using the user ID key and while joining, I had to give suffix characters, in order for it to get created. The following is the command I used,

a.join(b,how='inner', on='userId',lsuffix="_1")

If I dont use this suffix, I am getting error. But I dont want the column names to change because, that is causing a problem while running other analysis. So I want to remove this "_1" character from all the column names of the resulting dataframe. Can anybody suggest me an efficient way to remove last two characters of names of all the columns in the Pandas dataframe?

Thanks

Thtu

This snippet should get the job done :

df.columns = pd.Index(map(lambda x : str(x)[:-2], df.columns))

Edit : This is a better way to do it

df.rename(columns = lambda x : str(x)[:-2])

In both cases, all we're doing is iterating through the columns and apply some function. In this case, the function converts something into a string and takes everything up until the last two characters.

I'm sure there are a few other ways you could do this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove last two characters from column names of all the columns in Dataframe - Pandas

From Dev

remove first two characters for all column names in a data frame in R

From Dev

Drop columns from pandas dataframe, regardless of whether ALL column names are present

From Dev

Drop columns from pandas dataframe, regardless of whether ALL column names are present

From Dev

Select rows from a DataFrame based on last characters of values in a column in pandas

From Dev

python pandas selecting columns from a dataframe via a list of column names

From Dev

python pandas selecting columns from a dataframe via a list of column names

From Dev

remove last few characters in PySpark dataframe column

From Dev

pandas merge to bring two columns from a dataframe and doing operations on column

From Dev

How can I delete the last two elements from series characters (column names)?

From Dev

Remove last two characters from a string if it is -C

From Dev

Remove all the characters from string after last '/'

From Dev

Remove all the characters from string after last '/'

From Dev

Remove the characters after 64 characters of column names in pandas

From Dev

Remove last two characters

From Dev

How to remove '.' from column names in a dataframe?

From Dev

How to remove ".x" from column names in the dataframe?

From Dev

How to merge the two columns from two dataframe into one column of a new dataframe (pandas)?

From Dev

Taking last characters of a column of objects and making it the column on a dataframe - pandas python

From Dev

Remove all characters except last n characters from a string

From Dev

Remove all characters except last n characters from a string

From Dev

Pandas subtract each column in dataframe_a from all columns of dataframe_b and write result to third dataframe

From Dev

Splitting a Pandas DataFrame column into two columns

From Dev

Build list from column names Pandas DataFrame

From Dev

what is the fast way to drop columns in pandas dataframe from a list of column names

From Java

Python : Remove all data from a column of a dataframe except the last value that we store in the first row

From Dev

Creating new dataframe columns from existing dataframe column names

From Dev

Remove all characters from a string from last comma onwards

From Dev

R How to remove characters from long column names in a data frame

Related Related

  1. 1

    Remove last two characters from column names of all the columns in Dataframe - Pandas

  2. 2

    remove first two characters for all column names in a data frame in R

  3. 3

    Drop columns from pandas dataframe, regardless of whether ALL column names are present

  4. 4

    Drop columns from pandas dataframe, regardless of whether ALL column names are present

  5. 5

    Select rows from a DataFrame based on last characters of values in a column in pandas

  6. 6

    python pandas selecting columns from a dataframe via a list of column names

  7. 7

    python pandas selecting columns from a dataframe via a list of column names

  8. 8

    remove last few characters in PySpark dataframe column

  9. 9

    pandas merge to bring two columns from a dataframe and doing operations on column

  10. 10

    How can I delete the last two elements from series characters (column names)?

  11. 11

    Remove last two characters from a string if it is -C

  12. 12

    Remove all the characters from string after last '/'

  13. 13

    Remove all the characters from string after last '/'

  14. 14

    Remove the characters after 64 characters of column names in pandas

  15. 15

    Remove last two characters

  16. 16

    How to remove '.' from column names in a dataframe?

  17. 17

    How to remove ".x" from column names in the dataframe?

  18. 18

    How to merge the two columns from two dataframe into one column of a new dataframe (pandas)?

  19. 19

    Taking last characters of a column of objects and making it the column on a dataframe - pandas python

  20. 20

    Remove all characters except last n characters from a string

  21. 21

    Remove all characters except last n characters from a string

  22. 22

    Pandas subtract each column in dataframe_a from all columns of dataframe_b and write result to third dataframe

  23. 23

    Splitting a Pandas DataFrame column into two columns

  24. 24

    Build list from column names Pandas DataFrame

  25. 25

    what is the fast way to drop columns in pandas dataframe from a list of column names

  26. 26

    Python : Remove all data from a column of a dataframe except the last value that we store in the first row

  27. 27

    Creating new dataframe columns from existing dataframe column names

  28. 28

    Remove all characters from a string from last comma onwards

  29. 29

    R How to remove characters from long column names in a data frame

HotTag

Archive