Home > other >  How to remove last characters for some but not all columns of a pandas dataframe
How to remove last characters for some but not all columns of a pandas dataframe

Time:09-29

I am trying to format multiple columns in a pd dataframe by removing the the last six characters and making all lines proper case. I know how to do it on a single column but can't figure out a clean way of doing it in multiple columns without coding one line for each column. Note these are only 4 of the 20 columns in the dataframe.

QA Support UA Support Dev Support Prod Support
Stephen Andrews AB1603 Scott Barker AB2044 Edward Boyes AB1548 David Nesmith AB1996
Ruth Xavier AB1594 LENA Kelly AB1708 Angela NEVIN AB1242 Renee ALLEN AB2162
Ryan Trussell AB3102 Roland Irons AB1772 Aaron Nixon AB1249 George Folkes AB3400
   df['QA Support'] = df['QA Support'].str[:-6].str.title()
   df['UA Support'] = df['UA Support'].str[:-6].str.title()
   df['Dev Support'] = df['Dev Support'].str[:-6].str.title()
   df['Prod Support'] = df['Prod Support'].str[:-6].str.title()

How can I make the change to multiple columns in a single line of code?

CodePudding user response:

Dataframes do not have the str methods, only series, so you cannot apply them to a dataframe (or subset of columns) all at once. However, you can stack the columns into a single series with multi-index, transform them and unstack them.

df = pd.DataFrame({"QA Support": ["Stephen Andrews AB1603", "Ruth Xavier AB1594", "Ryan Trussell AB3102"], "UA Support": ["Scott Barker AB2044", "LENA Kelly AB1708","Roland Irons AB1772"]})
columns = ["QA Support", "UA Support"]
df[columns] = df[columns].stack().str[:-6].str.title().unstack()

Alternatively, use apply to apply the same function to several columns.

df[columns] = df[columns].apply(lambda s: s.str[:-6].str.title())

Finally, you could just use a conventional for loop:

for column in ["QA Support", "UA Support"]:
    df[column] = df[column].str[:-6].str.title()
  • Related