パンダのデータフレームを
index A
0 1qwe 3asd
1 6qwe 35asd
2 11qwe 13asd
3 17qwe 8asd
4 5qwe 9asd
5 7qwe 2asd
6 1qwe 20asd
A.dtype = object
このデータフレームを次のように変換します
index A
0 1.03
1 6.35
2 11.13
3 17.08
4 5.09
5 7.02
6 1.20
A.dtype = float64
Pythonで変換は可能ですか?はいの場合、効率的な方法でコードを提供してください。
ピリオド(。)の後、1桁しかない場合は、ゼロのパディングを行う必要があります。
これは1回のstr.replace
呼び出しで実行できます。
df['A'].str.replace(r'(\d+).*?(\d+).*', lambda x: '{}.{:0>2}'.format(x[1], x[2]))
pd.to_numeric(df['A'].str.replace(
r'(\d+).*?(\d+).*', lambda x: '{}.{:0>2}'.format(x[1], x[2])), errors='coerce')
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加