我有一个带有列的数据框,其中每行包含一个列表。我想知道从列表列中识别模式最有效的方法/最佳实践是什么——例如,接受前的平均拒绝数。(见下面的示例)
sequence_of_selection
0 Accept,Reject,Reject,Reject,Reject,Accept,Reje...
1 Accept,Reject,Reject,Reject,Reject,Reject,Reje...
2 Reject,Accept,Accept,Reject,Reject,Reject,Acce...
3 Accept,Reject,Accept,Accept,Accept,Accept,Reje...
4 Reject,Accept,Reject,Accept,Reject,Reject,Acce...
我可以将数据转换为字符串并拆分它们,或者在字符串中搜索子字符串等等,但是我更愿意寻找一种更有效的方法,因为Python字符串是不可变的
任何建议/帮助都将不胜感激
这是一个非常好的问题我想强调事件的时间间隔的力量——基于发布的序列,人们对行为和可预测性有很多洞察。考虑到这一点,我写了一个很长的答案,希望能解释一些数据操作的核心原则
1。创建自定义函数以执行计算:
(假设您只应用于一个列表–我建议在调试或测试时提取一个列表)
2。将此自定义函数应用于df:
pd.to_Series
转换为多列。使用pd.merge
添加到原始df
李>因为它们是列表,所以可以获取
'Accept'
的index
,然后取这些索引的平均值。如果索引为0,则列表中的第一项为'Accept'
,因此在它之前有零'Reject'
,依此类推相关问题 更多 >
编程相关推荐