使用panda在python中加入colums

ctehm74n  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(301)

我正在尝试将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","2 Month Date","3 Month Date","4 Month Date","5 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","2 Month Date","3 Month Date","4 Month Date","5 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'
niwlg2el

niwlg2el1#

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

上一行给出了Pandas1.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将返回一个Dataframe。实际上,您正在为一个系列分配一个Dataframe。
要联接列值,可以使用 apply 在具有的行上 axis=1 ,然后将行值转换为数组,并使用适当的分隔符将它们连接起来。

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

相关问题