【发布时间】:2019-09-03 09:22:42
【问题描述】:
所以我知道我可以像这样在 Pandas 中简单地添加一个新列:
df
=====
A
1 5
2 6
3 7
df['new_col'] = "text"
df
====
A new_col
1 5 text
2 6 text
3 7 text
而且我还可以根据对现有列的操作设置新列。
def times_two(x):
return x * 2
df['newer_col'] = time_two(df.a)
df
====
A new_col newer_col
1 5 text 10
2 6 text 12
3 7 text 14
但是,当我尝试对文本列进行操作时,我得到了一个意外的 AttributeError。
df['new_text'] = df['new_col'].upper()
AttributeError: 'Series' object has no attribute 'upper'
它现在将值视为一个系列,而不是那个“单元格”中的值。
为什么文本会发生这种情况而不是数字会发生这种情况?如何使用基于现有文本列的新列更新我的 DF?
【问题讨论】: