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
CodePudding user response:
I realize that object
is not a problem, instead is the type that pandas use for string or mixed types (https://pbpython.com/pandas_dtypes.html). More precisely:
Pandas dtype | Python type | NumPy type | Usage |
---|---|---|---|
object | str or mixed | string_, unicode_, mixed types | Text or mixed numeric and non-numeric values |
int64 | int | int_, int8, int16, int32, int64, uint8, uint16, uint32, uint64 | Integer numbers |
float64 | float | float_, float16, float32, float64 | Floating point numbers |
Infact if I print the type of a single cell it gives me str
type(UFC_db.at[0,'Referee'])
str
and as a string I can change it with another string
UFC_db.at[0,'Referee'] = 'XXXXXX'
UFC_db.at[0,'Referee']
'XXXXXX'