我有以下数据框架:
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
我正在尝试转换列的类型win_by从object
到str
。
我使用.astype()
在这里建议如何转换列与dtype作为对象到字符串在熊猫数据框架:
UFC_db['win_by'] = UFC_db['win_by'].astype('|S')
但没有改变:
<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
我也试过
UFC_db['win_by'] = UFC_db['win_by'].astype('str')
UFC_db['win_by'] = UFC_db['win_by'].astype(str)
和
UFC_db['win_by'] = UFC_db['win_by'].astype('str',errors ="ignore")
Python - pandas列类型强制转换使用"astype"不工作
但仍无变化
我尝试了许多方法,但我发现这是唯一适合我的方法,并将对象转换为文本
df['column'].astype('string')
我意识到object
不是问题,而是熊猫用于字符串或混合类型的类型(https://pbpython.com/pandas_dtypes.html)。更准确地说应该是:
Pandas dtype | Python类型 | NumPy类型 | 用法 |
---|---|---|---|
object | str或mixed | string_, unicode_,混合类型 | 文本或混合数值和非数值 |
int64 | int | int_, int8, int16, int32, int64, uint8, uint16, uint32, uint64 | 整数 |
float64 | float | float_, float16, float32, float64 | 浮点数 |