使用数据框时替换变量

时间:2018-07-05 12:41:32

标签: pandas iteration python-3.6 substitution

我正在尝试遍历多列的to_datetime格式并创建带有前缀的新列。我似乎遇到的问题是将列标题替换为to_datetime命令。手动执行以下命令:-

pipeline['pyCreated_Date'] = pd.to_datetime(pipeline.Created_Date, errors='raise')

但是当我尝试进行迭代时会得到Attribute Error: 'DataFrame' object has no attribute 'dh'。我已经搜索了答案,并根据Renaming pandas data frame columns using a for loop尝试了各种尝试,但似乎缺少基本信息。这是我最近的代码:-

date_header = ['Created_Date', 'End_Date', 'Expected_Book_Date', 'Last_Modified_Date',
               'Start_Date', 'Workspace_Won/Lost_Date', 'pyCreated_Date']
for dh in date_header:
    pipeline['py' + dh.format()] = pd.to_datetime(
               pipeline.dh.format(), errors='raise')

似乎出现dh未被识别为错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-121-d00bf0a5a7fd> in <module>()
      3 date_header = ['Created_Date', 'End_Date', 'Expected_Book_Date', 'Last_Modified_Date', 'Start_Date', 'Workspace_Won/Lost_Date']
      4 for dh in date_header:
----> 5     pipeline['py' + dh.format()] = pd.to_datetime(pipeline.dh.format(), errors='raise')

/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   4370             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4371                 return self[name]
-> 4372             return object.__getattribute__(self, name)
   4373 
   4374     def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'dh'

请实现此目的的正确语法是什么?抱歉,这是菜鸟的错误,但感谢您的支持。

非常感谢

在ALollz的帮助之后更新!

这是终于奏效的

for col_name in date_header:
    pipeline['py'+ col_name.format()] = pd.to_datetime(pipeline[col_name], errors='coerce')
print(f"{pipeline['py'+ col_name.format()].value_counts(dropna=False)}")

0 个答案:

没有答案