使用 Panda 在 Python 中加入 Colums

问题描述

我正在尝试将 6 个月的表格加入 Python 中的一个列。但是我不知道为什么它给我带来这么多麻烦。有什么帮助吗?

df.Dates = df[["1 Month Date","2 Month Date","3 Month Date","4 Month Date","5 Month Date","6 Month Date"]]
df.Dates = pd.to_datetime(df['Dates'],format='%Y-%m-%d %H:%M:%s')
print(df.Dates)

这里是错误

    KeyError                                  Traceback (most recent call last)
<ipython-input-23-83667cbe6215> in <module>
      1 df.Dates = df[["1 Month Date","6 Month Date"]]
----> 2 df.Dates = pd.to_datetime(df['Dates'],format='%Y-%m-%d %H:%M:%s')
      3 print(df.Dates)
      4 #Dates = df[["1 Month Date","6 Month Date"]].apply(pd.Series.explode).sum(axis=1)
      5 #print(Dates)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self,key)
   2900             if self.columns.nlevels > 1:
   2901                 return self._getitem_multilevel(key)
-> 2902             indexer = self.columns.get_loc(key)
   2903             if is_integer(indexer):
   2904                 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,key,method,tolerance)
   2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err
   2898 
   2899         if tolerance is not None:

KeyError: 'Dates'

解决方法

df.Dates = df[["1 Month Date","2 Month Date","3 Month Date","4 Month Date","5 Month Date","6 Month Date"]]

上面这行对 pandas 1.2.3 发出警告

UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access

因此要创建新列,最好使用 df['Dates']

此外,通过使用列列表来选择列,pandas 将返回一个数据框。您实际上是将数据帧分配给系列。

要连接列值,您可以在带有 apply 的行上使用 axis=1,然后将行值转换为数组并使用适当的分隔符连接它们。

cols = ["1 Month Date","6 Month Date"]
df['Dates'] = df[cols].apply(lambda row: ''.join(row.values.astype(str)),axis=1)