【问题标题】:Joining Colums in Python using Panda使用 Panda 在 Python 中连接列
【发布时间】:2021-04-27 10:24:34
【问题描述】:

我正在尝试将 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'

【问题讨论】:

  • 你能添加一些示例数据吗? 3 行 3 列并添加预期输出?
  • 第一行的右侧将为您提供无法在左侧使用 df.date 或 df['date'] 的数据框,它将无法正常工作。我猜你想连接所有这些列,请使用 str 方法
  • col_list = ["1 个月日期","2 个月日期","3 个月日期","4 个月日期","5 个月日期","6 个月日期"] df.Date = df.apply(lambda x: ''.join(str(x[col]) for col in col_list), axis=1)

标签: python pandas dataframe keyerror


【解决方案1】:
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 将返回一个数据框。您实际上是在将一个数据框分配给一个系列。

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

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)

【讨论】:

  • 当我打印它并没有给我预期的输出,这是 6 个月内所有日期的列表 ::
  • 0 2020-09-01T00:00:00.0000000002020-10-01T00:00:... 1 2020-09-01T00:00:00.0000000002020-10-01T00:00:... 2 2020 -09-01T00:00:00.0000000002020-10-01T00:00:... 3 2020-09-01T00:00:00.0000000002020-10-01T00:00:... 4 2020-09-01T00:00:00.0000000002 -01T00:00:... 5 2020-09-01T00:00:00.0000000002020-10-01T00:00:... 7 2020-09-01T00:00:00.0000000002020-10-01T00:00:... 8 2020 -09-01T00:00:00.0000000002020-10-01T00:00:... 9 2020-09-01T00:00:00.0000000002020-10-01T00:00:... 10 2020-09-01T00:00:00.00000000010001 -01T00:00:... 名称:日期,数据类型:对象
  • 加上它会产生一个对象,但我需要日期时间
  • @XIUJY1111 加入是什么意思?全部追加到一列?
猜你喜欢
  • 2018-08-14
  • 2020-06-10
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
相关资源
最近更新 更多