Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: convert object to str

I have the following dataframe:

    R_fighter       B_fighter           win_by                  last_round  Referee         date            Winner
0   Adrian Yanez    Gustavo Lopez       KO/TKO                  3           Chris Tognoni   March 20, 2021  Adrian Yanez
1   Trevin Giles    Roman Dolidze       Decision - Unanimous    3           Herb Dean       March 20, 2021  Trevin Giles
2   Tai Tuivasa     Harry Hunsucker     KO/TKO                  1           Herb Dean       March 20, 2021  Tai Tuivasa
3   Cheyanne Buys   Montserrat Conejo   Decision - Unanimous    3           Mark Smith      March 20, 2021  Montserrat Conejo
4   Marion Reneau   Macy Chiasson       Decision - Unanimous    3           Mark Smith      March 20, 2021  Macy Chiasson

I'm trying to convert the type of the column win_by from object to str.
I use .astype() as suggested here How to convert column with dtype as object to string in Pandas Dataframe:

UFC_db['win_by'] = UFC_db['win_by'].astype('|S')

but nothing changed:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6012 entries, 0 to 6011
Data columns (total 7 columns):
 #   Column      Non-Null Count  Dtype 
---  ------      --------------  ----- 
 0   R_fighter   6012 non-null   object
 1   B_fighter   6012 non-null   object
 2   win_by      6012 non-null   object
 3   last_round  6012 non-null   object
 4   Referee     6012 non-null   object
 5   date        6012 non-null   object
 6   Winner      6012 non-null   object
dtypes: object(7)
memory usage: 328.9+ KB

I also tried

UFC_db['win_by'] = UFC_db['win_by'].astype('str')

UFC_db['win_by'] = UFC_db['win_by'].astype(str)

and

UFC_db['win_by'] = UFC_db['win_by'].astype('str',errors ="ignore")

as suggested here Python - pandas column type casting with "astype" is not working

but still nothing changed

like image 569
paolopazzo Avatar asked Oct 24 '25 04:10

paolopazzo


1 Answers

I tried many methods, but I found that this is the only method that worked for me and convert the object into text

df['column'].astype('string') 
like image 196
sara ali Avatar answered Oct 26 '25 01:10

sara ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!