pandas 检查 Dataframe 列中的元素是否具有相同类型

4szc88ey  于 2023-03-21  发布在  其他
关注(0)|答案(1)|浏览(110)

我使用Python并使用 Dataframe df。当尝试检查所有列的每行是否具有相同的类型时,我编写了以下代码行:

a=0
first_object = df.loc[df.index[0]]
for column in df: 
    for i in range(0,len(df)):
        if type(df[column][i]) != type(first_object[column]):
            a+=1
print(a)

我得到的错误是:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/opt/anaconda3/envs/adsml/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:

~/opt/anaconda3/envs/adsml/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

~/opt/anaconda3/envs/adsml/lib/python3.9/site-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 155

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/var/folders/xb/74q_24bx0rxgqc6gtd6ksn7c0000gn/T/ipykernel_25626/3160699232.py in <module>
      3 for column in df:
      4     for i in range(0,len(df)):
----> 5         if type(df[column][i]) != type(first_object[column]):
      6             a+=1

~/opt/anaconda3/envs/adsml/lib/python3.9/site-packages/pandas/core/series.py in __getitem__(self, key)
    940 
    941         elif key_is_scalar:
--> 942             return self._get_value(key)
    943 
    944         if is_hashable(key):

~/opt/anaconda3/envs/adsml/lib/python3.9/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
   1049 
   1050         # Similar to Index.get_value, but we do not fall back to positional
-> 1051         loc = self.index.get_loc(label)
   1052         return self.index._get_values_for_loc(self, loc, label)
   1053 

~/opt/anaconda3/envs/adsml/lib/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 155

我很困惑,因为type(df[column][i])type(first_object[column])是分开工作的。我尝试了匹配类型和非匹配类型,TrueFalse都按预期返回了。所以我不明白为什么我的代码不工作。

k10s72fa

k10s72fa1#

如果我理解正确的话,您需要计算具有唯一对象类型的列的数量。
您可以用途:

df.applymap(type).nunique().eq(1).sum()
修复代码:

我不会在现实生活中使用循环!

a=0
first_object = df.iloc[0]
for column in df: 
    for i in df.index:
        if type(df.loc[i, column]) != type(first_object[column]):
            a+=1

向量等价物(计算与第一行不同的值)为:

df2 = df.applymap(type)
out = df2.ne(df2.iloc[0]).sum().sum()

相关问题