将两列等长的合并为一

时间:2018-12-31 15:58:40

标签: python pandas dataframe series

我有一个包含多个列的pandas DataFrame。我要完成的工作是将两列的值合并/堆叠到一列中,将每一列的值一个接一个地堆叠(不幸的是,这一要求使我无法使用类似联合的解决方案)。其他其余列的内容可以重复。任何帮助都非常感激

TSearchableLookupService

2 个答案:

答案 0 :(得分:5)

您可以将价格和日期列设置为索引,并堆叠股票和股票代码。最后,使用reset_index进行一些清理。

df.set_index(['Date', 'Price'])[['Stock Ticker','Index Ticker']].stack()\
.reset_index(2,drop = True).reset_index(name = 'Stock and Bench')


    Date    Price   Stock and Bench
0   12/31/2018 8:57 100 AAPL
1   12/31/2018 8:57 100 INDX
2   12/31/2018 8:57 123 GOOG
3   12/31/2018 8:57 123 RSL
4   12/31/2018 8:57 90  GM
5   12/31/2018 8:57 90  COMP
6   12/31/2018 8:57 340 MMM
7   12/31/2018 8:57 340 NIKK
8   12/31/2018 8:57 30  INVD
9   12/31/2018 8:57 30  EUR

答案 1 :(得分:4)

您可以使用pd.meltDatePrice设置为id_vars

(df.melt(id_vars=['Date', 'Price'], 
         value_name='Stock and Bench')
         .drop('variable', axis=1))

         Date        Price     Stock and Bench
0  12/31/2018/8:57    100            AAPL
1  12/31/2018/8:57    123            GOOG
2  12/31/2018/8:57     90              GM
3  12/31/2018/8:57    340             MMM
4  12/31/2018/8:57     30            INVD
5  12/31/2018/8:57    100            INDX
6  12/31/2018/8:57    123             RSL
7  12/31/2018/8:57     90            COMP
8  12/31/2018/8:57    340            NIKK
9  12/31/2018/8:57     30             EUR

或使用pd.wide_to_long

(pd.wide_to_long(df.reset_index(), stubnames='Ticker', i = 'index', 
                j = 'num', suffix='\w+')
                .reset_index(drop=True)
                .rename({'Ticker':'Stock and Bench'}, axis=1))

        Date         Price Stock and Bench
0  12/31/2018-8:57    100   AAPL
1  12/31/2018-8:57    123   GOOG
2  12/31/2018-8:57     90     GM
3  12/31/2018-8:57    340    MMM
4  12/31/2018-8:57     30   INVD
5  12/31/2018-8:57    100   INDX
6  12/31/2018-8:57    123    RSL
7  12/31/2018-8:57     90   COMP
8  12/31/2018-8:57    340   NIKK
9  12/31/2018-8:57     30    EUR