Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have a dataframe with codes that are written with one letter, two numbers and one decimal ('A09.9'). To treat them I need to group it by removing the decimal from the whole table.

So far I can't think of any form (the dataframe is 40000x100). But I have to simplify this:

id code1 code2 code3 code4

0  A09.9 B25.3 A02.2  NaN

1  B29.3 J27.7 Z23.3 H35.2

2  C21.2 C03.5  NaN   NaN

to this:

id code1 code2 code3 code4

0   A09   B25   A02   NaN

1   B29   J27   Z23   H35

2   C21   C03   NaN   NaN

Thank you very much in advance!

1 Answer

0 votes
by (25.1k points)
edited by

Use the apply method on the dataframe, pass a lambda that slices the string up to first 3 characters. Like this:

df = df.apply(lambda x: x.str[:3])

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...