繁体   English   中英

无法通过使用预定义参数作为字符串的一部分来引用列

[英]Can't reference column by using predefined parameter as part of string

我有一个数据集,我想通过使用预定义参数作为字符串的一部分来引用我的列。 这样做的原因是我要保留的列会根据一年中的时间和年份而变化。

我的参数是:

year = '20'

这工作正常并给我想要的结果:

df.['Q1 FY20'] = df.['Q1 FY20'].astype('int32')

但是当我尝试用我的参数替换字符串中的“20”时,我得到 KeyError: 'Q1 FY20':

df.['Q1 FY' + year] = df.['Q1 FY' + year].astype('int32')

我真的不明白这个,因为我已经检查过:

type('Q1 FY20') == type('Q1 FY' + year)
'Q1 FY20' == 'Q1 FY' + year

...他们都是真的。 我究竟做错了什么?

这是完整的错误消息:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

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

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

KeyError: 'Q1 FY20'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-474-3a24ee57971a> in <module>
----> 1 df['Q1 FY' + year] = df['Q1 FY' + year].astype('int32')

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2993             if self.columns.nlevels > 1:
   2994                 return self._getitem_multilevel(key)
-> 2995             indexer = self.columns.get_loc(key)
   2996             if is_integer(indexer):
   2997                 indexer = [indexer]

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

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

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

KeyError: 'Q1 FY20'
```

你可以试试这个:

year = '20'
column_name = "Q1 FY" + year

df[column_name] = df[column_name].astype('int32')

快照: 结果

我发现了我的错误,我试图在执行所需的数据清理之前在一个实例中测试代码。

当我用我的参数替换实际代码时,它起作用了。 但是,我仍然不明白为什么在写出完整字符串时没有收到任何 KeyError。

非常感谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM