Find mixed types in Pandas columns

前端 未结 3 2048
闹比i
闹比i 2020-12-25 15:06

Ever so often I get this warning when parsing data files:

WARNING:py.warnings:/usr/local/python3/miniconda/lib/python3.4/site-
packages/pandas-0.16.0_12_gdcc         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 15:37

    I'm not entirely sure what you're after, but it's easy enough to find the rows which contain elements which don't share the type of the first row. For example:

    >>> df = pd.DataFrame({"A": np.arange(500), "B": np.arange(500.0)})
    >>> df.loc[321, "A"] = "Fred"
    >>> df.loc[325, "B"] = True
    >>> weird = (df.applymap(type) != df.iloc[0].apply(type)).any(axis=1)
    >>> df[weird]
            A     B
    321  Fred   321
    325   325  True
    

提交回复
热议问题