【问题标题】:Convert all elements of row to name of month and year将行的所有元素转换为月份和年份的名称
【发布时间】:2021-07-16 12:06:12
【问题描述】:

我想将第 4 行(索引 3)转换为月份和年份的名称

我有一行包含日期的数据框。它是 datetime.datetime 对象。我想将行的所有元素转换为月份和年份的名称。

【问题讨论】:

  • 你应该转置你的数据框。 Pandas 数据类型沿轴列定义。

标签: pandas datetime apply


【解决方案1】:

选择行,对其进行操作以提取年份和月份名称,然后替换:

datetime = pd.to_datetime(df.iloc[3])
month_year = datetime.apply(lambda x: x.month_name() + ", " + str(x.year))
df.iloc[3] = month_year

【讨论】:

  • 我使用了那个代码。我使用 apply 方法使用了类似的代码。我收到这个错误。 datetime = pd.to_datetime(df_12.iloc[3]) month_year = datetime.apply(lambda x: x.month_name() + ", " + str(x.year)) df_12.iloc[3] = month_year TypeError: 不支持+ 的操作数类型:'float' 和 'str'
  • 我把月份名转成字符串解决了。
  • 太好了,然后将我的答案标记为解决方案!
猜你喜欢
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多