Hel*_*rth 4 python types numeric python-3.x pandas
我有一个数据框,其中可能包含与数值混合的对象。
我的目标是将每个值更改为简单整数,但是其中一些值介于-数字之间。
一个最小的工作示例如下所示:
import pandas as pd
d = {'API':[float(4433), float(3344), 6666, '6-9-11', '8-0-11', 9990]}
df = pd.DataFrame(d)
Run Code Online (Sandbox Code Playgroud)
我尝试:
df['API'] = df['API'].str.replace('-','')
Run Code Online (Sandbox Code Playgroud)
但这给我留下了nan数字类型,因为它只在整个框架中搜索字符串。
输出是:
API
nan
nan
nan
6911
8011
nan
Run Code Online (Sandbox Code Playgroud)
我想要一个输出:
API
4433
3344
6666
6911
8011
9990
Run Code Online (Sandbox Code Playgroud)
所有类型都在的地方int。
有没有一种简单的方法可以只处理系列中的对象类型,但保持实际的数字不变?我在大型数据集(300,000 多行)上使用这种技术,因此类似lambda或 的东西series operations比循环搜索更受青睐。
df.replace与使用regex=True
df = df.replace('-', '', regex=True).astype(int)
API
0 4433
1 3344
2 6666
3 6911
4 8011
5 9990
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12089 次 |
| 最近记录: |