如何在每次列更改其字符串值时标记数据帧中的行?
例如:
输入
ColumnA ColumnB
1 Blue
2 Blue
3 Red
4 Red
5 Yellow
# diff won't work here with strings.... only works in numerical values
dataframe['changed'] = dataframe['ColumnB'].diff()
ColumnA ColumnB changed
1 Blue 0
2 Blue 0
3 Red 1
4 Red 0
5 Yellow 1
我通过
ne
而不是使用实际的!=
比较获得更好的性能:计时
使用以下设置生成更大的数据帧:
我有以下时间安排:
使用^{} 并比较:
对于我的作品,与^{} 相比,则
NaN
被替换0
,因为在没有值之前:由timings编辑另一个答案-最快的是使用
ne
:相关问题 更多 >
编程相关推荐