首页 > 解决方案 > 处理熊猫中带有逗号的值

问题描述

我有一个包含 8 列的 CSV 文件。在 8 列中,有 2 列的值包含,例如2,134

对于处理,我需要将数据转换为数字(浮点数)

df = pd.read_csv('data.csv')
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 90181 entries, 0 to 90180
Data columns (total 8 columns):
user_id                        90181 non-null object
location_id                    90181 non-null int64
is_shift_accepted              90181 non-null int64
shift_accepted_role            90179 non-null float64
shift_accepted_specialities    89973 non-null float64
distance                       90144 non-null object
years_of_experience            80604 non-null float64
shift_id                       90181 non-null object
dtypes: float64(3), int64(2), object(3)
memory usage: 5.5+ MB

现在让我们转换成数字

df = df.convert_objects(convert_numeric=True)
df.dtypes
user_id                        float64
location_id                      int64
is_shift_accepted                int64
shift_accepted_role            float64
shift_accepted_specialities    float64
distance                       float64
years_of_experience            float64
shift_id                       float64
dtype: object

现在检查空值 -

# checking for missing values if any
df.isnull().sum()
user_id                        89943
location_id                        0
is_shift_accepted                  0
shift_accepted_role                2
shift_accepted_specialities      208
distance                         249
years_of_experience             9577
shift_id                       90042
dtype: int64

这里user_id和它们shift_id中的值一样,,给出最高的空计数,尽管它有一个非空值。是因为,存在于其中吗?预处理这些数据的正确方法是什么?

这是数据的样子

在此处输入图像描述

标签: pythonpandas

解决方案


推荐阅读