我想從 DataFrame 列“'CA Distance Nominal (LD | au)”中獲取左值 (LD) 管道分隔值,這是代碼。當我將字串轉換為浮點數時,我將所有值都設為 NaN。
cneos = pd.read_csv('cneos.csv')
print(cneos['CA Distance Nominal (LD | au)'].head())
cneos['Distance']=pd.to_numeric(cneos['CA Distance Nominal (LD | au)'], errors='coerce')
print(cneos['Distance'].head())
結果
0 2.02 | 0.00520
1 0.39 | 0.00100
2 8.98 | 0.02307
3 3.88 | 0.00996
4 4.84 | 0.01244
Name: CA Distance Nominal (LD | au), dtype: object
在 to_numeric() 之后
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
Name: Distance, dtype: float64
我怎樣才能得到兩個值 LD 和 AU 在浮點數中分開
uj5u.com熱心網友回復:
我不確定這是否是解決您的問題的最佳方法,但它確實有效:
separeted_data_frame = pd.DataFrame(cneos['CA Distance Nominal (LD | au)'].apply(lambda x: x.split('|')).to_list())
separeted_data_frame.columns = ['LD', 'AU']
separeted_data_frame.LD = separeted_data_frame.LD.astype(float)
separeted_data_frame.AU = separeted_data_frame.AU.astype(float)
cneos = cneos.join(separeted_data_frame).drop('CA Distance Nominal (LD | au)', 1)
結果是:
LD AU
0 2.02 0.00520
1 0.39 0.00100
2 8.98 0.02307
3 3.88 0.00996
4 4.84 0.01244
是你想要的嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450713.html
