【发布时间】:2018-03-16 11:50:34
【问题描述】:
我有一个数据框中的列列表,我想遍历这些列并对它们执行操作。这些列包含日期时间或什么都没有。
对于列表中的每一列,我想将列中包含“20”的每个值修剪为前 10 个字符,否则保持原样。
我已经尝试了几种方法,但得到了各种错误或不完美的结果。
以下版本会抛出“'str' object has no attribute 'apply'”的错误,但如果我不使用“.astype(str)”,则会收到“argument of type 'datetime”的错误.datetime' 不可迭代”。
df_combined[dateColumns] = df_combined[dateColumns].fillna(notFoundText).astype(str)
print (dateColumns)
for column in dateColumns:
for row in range(len(column)):
print(df_combined[column][row])
if "20" in (df_combined[column][row]):
df_combined[column][row].apply(lambda x: x[:10], axis=1)
print(df_combined[column][row])
停下来。提前致谢。
【问题讨论】:
标签: python string pandas numpy datetime