Antonio López Ruiz
Antonio López Ruiz

Reputation: 1466

Remove everything after a space - Pyspark

I have a dataframe df as follows:

    A               B
  21k2 b            1
  2412 9            p

Both A and B are strings.

I would like for the A column elements to be trimmed as follows:

  A               B
21k2              1
2412              p

Extra thank you points if you can also show how to remove anything before a space.

Upvotes: 0

Views: 875

Answers (1)

过过招
过过招

Reputation: 4189

You can use the split function and getItem method.

df = df.select(F.split('A', ' ').getItem(0).alias('A'), 'B')

Upvotes: 1

Related Questions