我有一个如下所示的数据框,只有2 category A
。每个名称可以与每行中的任何一个相关联。当名称再次出现时,我需要其他2列,表示该名称的最后5次交易中该名称的category B
和"-1"
的计数。如果之前的事务少于5个,则输出应为Document Number Date Due Name Category
21175370 9/20/17 Max A
20762835 8/20/17 Max A
21134931 8/25/17 Emily B
20784776 8/19/17 John A
20874477 8/4/17 Doe B
20874568 8/4/17 Doe B
20874575 8/4/17 Doe B
19784032 5/9/17 Doe B
19980723 5/25/17 Doe B
20833755 9/16/17 Doe A
。所以,我通过对名称进行排序然后在Date Due上对名称进行了数据处理,但不确定如何继续进行。
Document Number Date Due Name Category Category_A_Count Category_B_Count
21175370 9/20/17 Max A -1 -1
20762835 8/20/17 Max A -1 -1
21134931 8/25/17 Emily B -1 -1
20784776 8/19/17 Doe A -1 -1
20874477 8/4/17 Doe B -1 -1
20874568 8/4/17 Doe B -1 -1
20874575 8/4/17 Doe B -1 -1
19784032 5/9/17 Doe B -1 -1
19980723 5/25/17 Doe B 1 4
20833755 9/16/17 Doe A 0 5
输出如下:
vendorList = list(vendorLast5["Name"])
cat = list(vendorLast5["Category"])
print type(vendorList)
catA = [-1, -1, -1, -1]
catB = [-1, -1, -1, -1]
for i in range(0, len(vendorList)):
if(i > 4):
countA = 0
countB = 0
for j in range(i-5, i):
if(vendorList[j] != vendorList[i]):
countA = -1
countB = -1
break
if(cat[j] == "A"):
countA = countA+1
else:
countB=countB+1
catA.append(countA)
catB.append(countB)
我已经通过将pandas数据框更改为列表来尝试了一些东西,这可以完成工作,但我怎么能在熊猫中做到这一点? :
class Users: Object {
dynamic var phoneNumber:String = ""
dynamic var messageSenderName:String = ""
let userMessages = List<Messages>()
override static func primaryKey() -> String? {
return "phoneNumber"
}
// convenience init() here
}
class Messages: Object{
dynamic var id = UUID().uuidString
dynamic var phoneNumber:String = ""
dynamic var messageSenderName:String = ""
dynamic var messageBody:String = ""
dynamic var message_id:String = ""
dynamic var messageTime:String = ""
override static func primaryKey() -> String? {
return "id"
}
// convenience init here
}