代码之家  ›  专栏  ›  技术社区  ›  whatsinthename

如何获取和比较pyspark中两个数据帧中相似列的所有值的数据类型

  •  0
  • whatsinthename  · 技术社区  · 6 年前

    CSV 文件夹。考虑一个名为 Units Sold string 当我检查模式时。现在,我要遍历这两个数据帧中该列的每一行数据,并检查数据类型是否正确。

    如果列是 numeric 345 另一个是 345.00 然后高亮显示,因为这里的数据类型不同。我试过下面的方法,但很普通:

    types1=[df1.dtypes]
    #print(types1)
    types2 =[df2.dtypes]
    #print(types2)
    
    if(types1==types2):
        print("Data types is equal for both the files..")
    else:
        print("Data types is NOT equal for both the files..")  
    

    Units Sold
    8446
    3018
    1517
    3322
    9845
    9528
    

    我没有找到任何与我的问题相关的帖子。感谢您的帮助。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Skander HR    6 年前

    我也遇到了同样的问题,不得不在专栏上循环。我遇到的问题是,由于NA值指示符,一些数字列有时被读取为字符串。

    df1_types = pd.DataFrame(df1.dtypes,columns=["colname", "type"])
    df2_types = pd.DataFrame(df2.dtypes,columns=["colname", "type"])
    flag = True
    for index, row in df1_types.iterrows():
        if row['type'] != df2_types[df2_types['colname'] == row['colname']]['type'].values[0]:
            # Add numeric check here if needed
            flag = False
            break
    if flag:
        print("Data types is equal for both the files..")
    else:
        print("Data types is NOT equal for both the files..")