I want to convert a columns where the elements have a type liked number mixed with character, and I want to convert the column to Integer
type.
Input:
df = pd.DataFrame({'id':['Q001','Q021']})
Output:
id
0 Q001
1 Q021
Expected
:
id idInt
0 Q001 1
1 Q021 21
Or use pd.Series.str.replace
with regex of '\D+'
being replaced with ''
in each string:
df['idInt']=df['id'].str.replace('\D+','').astype(int)
And now:
print(df)
Is:
id idInt
0 Q001 1
1 Q021 21
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With