【问题标题】:How to find data type error in pandas dataframe?如何在熊猫数据框中查找数据类型错误?
【发布时间】:2020-04-09 05:28:33
【问题描述】:

df1:

  product   product_Id   Price
0 Mobile      G67129     4500
1 Earphone    H56438     8900
2 Heater      K12346     fgdht
3 Kitchen     566578     4500
4 4359        Gh1907     5674
5 plastic     G67129     Dfz67

df2:

  Column_Name   Expected_Dtype
0 product          String
1 product_Id       String
2 Price            int

我需要从 df1 中找出数据类型错误值,并在 df2 中有列数据类型信息。

输出:

   column_Name  Value  Exp_dtype index
0  product       4359  String    4
1  product_Id   566578 String    3
2  Price       fgdht    int      2
3  Price       Dfz67    int      5

【问题讨论】:

标签: python python-3.x pandas python-2.7


【解决方案1】:

由于这些类型混合在一起,都是对象,我只能想到使用 str match 和 regex 模式来挑选错误类型。

这是我的解决方案:

首先查找具有错误类型的行

bad_product = df['product'].loc[df['product'].str.match(r'[0-9.]+')]
bad_product_ID = df.product_Id.loc[df['product_Id'].str.match(r'[0-9.]+')]
bad_price = df.Price.loc[~df['Price'].str.match(r'[0-9.]+')]

将错误行全部连接在一起

df3 = pd.concat([bad_product,bad_product_ID,bad_price], axis=1).stack().reset_index()
df3.columns = ['index', 'Column_Name', 'value']

将其与 df2 合并

df2.set_index('Column_Name')
df3.set_index('Column_Name')
result = pd.merge(df3, df2, how='left')

结果:


  index Column_Name value   Expected_Dtype
0   2   Price       fgdht   int
1   3   product_Id  566578  String
2   4   product     4359    String
3   5   Price       Dfz67   int

当你一开始不知道时,试着把它分解成小任务。希望这会有所帮助。

【讨论】:

  • 盛壮感谢您的解决方案。现在您正在手动检查产品是字符串类型,因此您执行了检查。理想情况下,它应该从 df2 检索数据类型信息,然后执行检查。
  • 我知道,这并不理想,但是我找不到检查每个条目的dtype的方法,.dtypes只能检查整个系列。
【解决方案2】:

数据帧

import pandas as pd
import re
data = {'A':['1','2.0','4b','dog'], 'B':['12','tom','lom','so'],'C':['dog','tom','jerry','55']}
df = pd.DataFrame(data=data)
print(df)

    A   B   C
0   1   12  dog
1   2.0 tom tom
2   4b  lom jerry
3   dog so  55

现在分配字符串或 int/float 列的列表

int_col = ['A'] #I am considering col A numerical
string_col = ['B','C'] # col B & C as string

有趣的部分

num_pattern = "^\d+\.?\d*$" #this identify int and float both
int_float = []
errors_num = []
errors_string = []
for col in df[int_col].columns: #here detecting errors in numerical col
  i = 0
  for cells in df[int_col][col]:
    if re.findall(num_pattern,cells):
      int_float.append(cells)
    if cells not in int_float:
      errors_num.append({"column":col, "errors":cells, "index": i,'correct_datatype': 'float'})
    i += 1

for col in df[string_col].columns: #here detecting errors in string col
  i = 0
  for cells in df[string_col][col]:
    if re.findall(num_pattern,cells):
      errors_string.append({"column":col, "errors":cells, "index": i, 'correct_datatype': 'string'})
    i += 1

现在我们有 2 个关于列数据类型的错误列表。我们将加入名单 并打印 DataFrame,它将打印 Col_nameerrorindex numcorrect Data type

pd.DataFrame(data=errors_string + errors_num)

  column    errors  index   correct_datatype
0   B       12         0    string
1   C       55         3    string
2   A       4b         2    float
3   A       dog        3    float

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2019-01-26
    • 2018-11-22
    • 2016-10-18
    • 2018-10-12
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多