【发布时间】:2020-11-10 12:40:11
【问题描述】:
我正在尝试编写一个dict 理解,它创建一个dict,其中k,v 对是column_title, # of null values in that column,我的pandas df。
我有以下几点:
df1_null_dict = {c:df1.c.isnull().sum() for c in df1}
下面是 this example 如何在 Python 中构建字典推导式。
但是,我得到:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-a4c7d72985f6> in <module>
----> 1 df1_null_dict = {c:df1.c.isnull().sum() for c in df1}
2 df2_null_dict = {c:df2.c.isnull().sum() for c in df2}
<ipython-input-5-a4c7d72985f6> in <dictcomp>(.0)
----> 1 df1_null_dict = {c:df1.c.isnull().sum() for c in df1}
2 df2_null_dict = {c:df2.c.isnull().sum() for c in df2}
c:\python367-64\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5137 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5138 return self[name]
-> 5139 return object.__getattribute__(self, name)
5140
5141 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'c'
如何使用 dict 理解来实现我想要的结果?
【问题讨论】:
标签: python python-3.x pandas dataframe dictionary