【发布时间】:2019-10-23 04:54:22
【问题描述】:
我有这个数据框:
df = pd.DataFrame({"X" : ["2017-12-17","2017-12-18","2017-12-19"],
"Y": ["F","W","Q"]})
我正在寻找key 专栏:
X Y key
0 2017-12-17 F 2017-12-17_F
1 2017-12-18 W 2017-12-18_W
2 2017-12-19 Q 2017-12-19_Q
我已经尝试过1,2,3,最好的解决方案是(为了速度,因为它们接近 100 万行):
df.assign(key=[str(x) + "_" + y for x, y in zip(df["X"], df["Y"])])
它给了我这个错误:
TypeError: unsupported operand type(s) for +: 'Timestamp' and 'str'
为什么?
【问题讨论】:
-
其中一个值似乎不是
str类型。使用您提供的示例代码,都是字符串。但也许你的实际数据不是。 -
找出哪个,并转换为字符串:stackoverflow.com/questions/10624937/…
标签: python pandas numpy data-manipulation