and Happy new year!
Currently I'm dealing with a dataframe column as followed, with focus on the ACCOUNT_NUMBER column (type 'object'):
BRANCH_CODE ACCOUNT_NUMBER ACCOUNT_HEAD SCHEDULE_DUE_DATE \
1 00001838002 211102000 NaT
1 00001867003 211102000 NaT
1 00001962020 211102000 NaT
1 030MZAX9082 211102000 NaT
1 00002404079 211102000 NaT
1 00010322002 211102000 NaT
1 00021070011 211102000 NaT
1 091QWEV34QA 211102000 NaT
1 00024605002 211102000 NaT
1 214QSVC45AX 211102000 NaT
As you can see, some values in ACCOUNT_NUMBER are exclusively numbers, and some are a mixture of numbers and letters. My objective is to convert values that are exclusively numbers from 'object' to 'int', and keep the values that are mixture of numbers and letters as 'object' (ignore). To that end, I'm trying this code line:
file_consol['ACCOUNT_NUMBER']=file_consol['ACCOUNT_NUMBER'].astype(int,errors='ignore')
However, the code line fails to convert the numbers into int. I have tried another alternative below, but it also doesn't work:
file_consol_P['ACCOUNT_NUMBER']=file_consol_P['ACCOUNT_NUMBER'].astype(str).astype(int,errors='ignore')
Any help or explanation on this is greatly appreciated. Thank you!
Try with apply:
file_consol['ACCOUNT_NUMBER']=file_consol['ACCOUNT_NUMBER'].apply(lambda x: int(x) if all(k.isdigit() for k in x) else x)
Output:
0 1838002
1 1867003
2 1962020
3 030MZAX9082
4 2404079
5 10322002
6 21070011
7 091QWEV34QA
8 24605002
9 214QSVC45AX
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.