Python 将宽柱重塑为长柱

Python 将宽柱重塑为长柱,python,pandas,Python,Pandas,我有一张像下面这样的照片 tno,tdate,buyprice,sellprice,qty,t1,t2 1,2017,10,20,5,teamA,teamB 2,2017,5,10,5,teamB,teamA 预期op: tno,tdate,buyprice,sellprice,qty,t1,t2 1,2017,10,20,5,teamA,NaN 1,2017,10,20,5,NaN,teamB 2,2017,5,10,5,teamB,NaN 2,2017,5,10,5,NaN,TeamA

我有一张像下面这样的照片

tno,tdate,buyprice,sellprice,qty,t1,t2
1,2017,10,20,5,teamA,teamB
2,2017,5,10,5,teamB,teamA
预期op:

tno,tdate,buyprice,sellprice,qty,t1,t2
1,2017,10,20,5,teamA,NaN
1,2017,10,20,5,NaN,teamB
2,2017,5,10,5,teamB,NaN
2,2017,5,10,5,NaN,TeamA
发生的事情是,我将两个不同团队之间的内部事务分离为两个不同事务中的一个

我试着使用
df.unstack()
,还读到我不知道如何告诉熊猫我想要取消堆叠

更新1:

问题的大背景是:

tno,tdate,buyprice,sellprice,qty,Buyerteam,Sellerteam
1,2017,10,20,5,teamA,teamB
2,2017,5,10,5,teamB,teamA
有两种类型的交易

  • 类型1。其中,Buyerteam=NaN或Sellerteam=NaN(决不能两者兼而有之) 我计算
    数量*(买入价或卖出价)
    来计算团队的支出。如果buyTeam为NaN,则我做
    qty*sellprice
    如果sellTeam=NaN,则我做qty*buyprice
  • 类型2。其中t1和t2都不是NaN(本问题中的情况) 我正在尝试将type2数据转换为type1数据。但是如果我不介绍NaN,我的
    quantity*(买入价或卖出价)
    条件将无法应用

  • 我希望我介绍NaN的意图是明确的。

    如果可能,请为团队提供一个输出栏:

    编辑:如果只有两个团队可以与一起使用,则最后一个对于相同的列顺序使用,最后一个:


    是否可以为所有团队提供一个输出列?为问题添加了一些详细信息抱歉,我脱机了。但我愿意帮助你们,虽然更好的是用这种方式创造新的问题。你能用期望的输出更改数据样本,以便更好地解释和验证我的或其他解决方案吗?当然@jazrael谢谢你的帮助,我更新了问题。只是NaN对我很重要。因此,预期的o/p需要与我在问题中所显示的一样
    df = pd.lreshape(df, {'t':['t1','t2']})
    print (df)
       buyprice  sellprice  tdate  tno      t
    0        10         20   2017    1  teamA
    1         5         10   2017    2  teamB
    2        10         20   2017    1  teamB
    3         5         10   2017    2  teamA
    
    df = pd.concat([df.drop('t2', axis=1), df.drop('t1', axis=1)], ignore_index=True)
           .reindex_axis(df.columns, 1)
           .sort_values(['tno','tdate'])
    print (df)
       tno  tdate  buyprice  sellprice     t1     t2
    0    1   2017        10         20  teamA    NaN
    2    1   2017        10         20    NaN  teamB
    1    2   2017         5         10  teamB    NaN
    3    2   2017         5         10    NaN  teamA