我是一个在 2.7 版上工作的 python 新手。以下是我正在使用的数据框示例。还有其他与问题无关的列,因此它们不包含在下面。
df = pd.DataFrame( { "Name" : ["BROD", "BROD", "BROD", "BROD", "SSBD" , "SSBD","SSBD","SSBD"] ,
"Digit" : ["F", "F", "T", "T", "F", "F", "T", "T"],
"ID": ["A","A","A","A","B","B","B","B"],
"Date": ["2/3/2010","2/3/2010","2/3/2010","2/3/2010","3/4/2007","3/4/2007","3/4/2007","3/4/2007"],
"Base" : ["CAD","CAD","CAD","CAD","CAD","CAD","CAD","CAD"],
"Term" : ["USD","USD","JPY","JPY","EUR","EUR","JPY","JPY"],
"Amt": [100.00,100.00,9082.00,9082.00,60.00,60.00,7387.80,7387.80]})
有多个重复值。每行代表交易的一个组成部分,ID 列将它们分组为一个交易。我想创建一个新的数据框,每笔交易只包含一行。数据框如下所示:
ID Date Name Buy Sell Buy Amt Sell Amt
A 2/3/2010 BROD USD JPY 100.00 9082.00
B 3/4/2007 SSBD EUR JPY 60.00 7387.80
对于每个 ID,如果 Digit = F,则 Term 列中的值位于 Buy 列中,Amt 列中的值位于 Buy Amt 列中。如果数字 = T,则 Term 列中的值位于 Sell 列中,Amt 列中的值位于 Sell Amount 列中。
请为我指出正确的方向,以最有效的方式解决这个问题。谢谢。
RISEBY
达令说
相关分类