1

I have a bunch of columns like this

District
________
State District 1
State District 2
State District 3
4th State House District
5th State House District
State District 6
...
State District 17

I want to transform each of these so it only contains the integer value:

District
________
1
2
3
4
5
6
...
17

How can this be done with Pandas? Is this even possible with Pandas or would I have to perform this transformation with SQL or some database language?

archingfork
  • 145
  • 1
  • 7

1 Answers1

2

A simple solution using str.replace would be to just strip off all non numeric characters:

df['District'] = df['District'].str.replace('\D', '')
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360