将新数据框中的多行分组

时间:2018-09-12 19:52:08

标签: python pandas numpy pandas-groupby

我是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列中。如果Digit = T,则将Term列中的值放在Sell列中,将Amt列中的值放在Sell Amount栏中。

请指出正确的方向,以最有效的方式解决此问题。谢谢。

2 个答案:

答案 0 :(得分:1)

您可以先使用np.where,然后再使用groupby

df['Buy'] = np.where((df['Digit'] == 'F'), df['Term'], np.nan)

df['Sell'] = np.where((df['Digit'] == 'T'), df['Term'], np.nan)

df['BuyAmt'] = np.where((df['Digit'] == 'F'), df['Amt'], np.nan)
df['SellAmt'] = np.where((df['Digit'] == 'T'), df['Amt'], np.nan)

df.drop(['Digit','Base','Term','Amt'], axis=1, inplace= True)

df = df.groupby('ID').first()

print(df)
    Name      Date  Buy Sell  BuyAmt  SellAmt
ID                                           
A   BROD  2/3/2010  USD  JPY   100.0   9082.0
B   SSBD  3/4/2007  EUR  JPY    60.0   7387.8

如果您还需要按发布顺序排列列,则可以使用pandas reindex

答案 1 :(得分:0)

我认为应该删除重复项,否则您需要更好地解释应如何处理相同的行:

>>> df2 = df.drop_duplicates().reset_index(drop=True)

然后,我们创建两个数据帧,一个用于“ F”,一个用于“ T”,分别创建Buy / SellBuy Amt / Sell Amt,并删除未使用的列:

>>> df_F = df2[df2.Digit == 'F'].assign(**{'Buy': lambda x: x.Term, 'Buy Amt': lambda x: x.Amt})
...                             .drop(['Digit', 'Base', 'Term', 'Amt'], axis=1)
>>> df_T = df2[df2.Digit == 'T'].assign(**{'Sell': lambda x: x.Term, 'Sell Amt': lambda x: x.Amt})
...                             .drop(['Digit', 'Base', 'Term', 'Amt'], axis=1)

最后,我们合并两个数据框,并重新排列列顺序:

>>> merged = df_F.merge(df_T, on=['ID', 'Name', 'Date'])
>>> merged[['ID', 'Date', 'Name', 'Buy', 'Sell', 'Buy Amt', 'Sell Amt']]
  ID      Date  Name  Buy Sell  Buy Amt  Sell Amt
0  A  2/3/2010  BROD  USD  JPY    100.0    9082.0
1  B  3/4/2007  SSBD  EUR  JPY     60.0    7387.8

就是这样。如果“ ID”应作为索引,则可以使用merged.set_index('ID')