简体   繁体   English

使用 Pandas 将列索引堆叠在一起

[英]Stacking column indices on top of one another using Pandas

I'm looking to stack the indices of some columns on top of one another, this is what I currently have:我希望将一些列的索引堆叠在一起,这就是我目前拥有的:

                          
            Buy     Buy Currency   Sell     Sell Currency   
Date                        
2013-12-31  100     CAD            100      USD
2014-01-02  200     USD            200      CAD
2014-01-03  300     CAD            300      USD
2014-01-06  400     USD            400      CAD

This is what I'm looking to achieve:这就是我想要实现的目标:

Buy/Sell     Buy/Sell Currency   
                     

100          USD
100          CAD
200          CAD
200          USD
300          USD
300          CAD

And so on, Basically want to take the values in "Buy" and "Buy Currency" and stack their values in the "Sell" and "Sell Currency" columns, one after the other.依此类推,基本上想要获取“买入”和“买入货币”中的值,并将它们的值一个接一个地堆叠在“卖出”和“卖出货币”列中。

And so on.等等。 I should mention that my data frame has 10 columns in total so using我应该提到我的数据框总共有 10 列,所以使用

df_pl.stack(level=0)

doesn't seem to work.似乎不起作用。

One option is with pivot_longer from pyjanitor , where for this particular use case, you pass a list of regular expressions (to names_pattern ) to aggregate the desired column labels into new groups (in names_to ):一种选择是使用pyjanitor中的pivot_longer ,对于这个特定的用例,您传递一个正则表达式列表(到names_pattern )以将所需的列标签聚合到新组中(在names_to中):

# pip install pyjanitor
import pandas as pd
import janitor

df.pivot_longer(index=None, 
                names_to = ['Buy/Sell', 'Buy/Sell Currency'], 
                names_pattern = [r"Buy$|Sell$", ".+Currency$"], 
                ignore_index = False, 
                sort_by_appearance=True)

            Buy/Sell Buy/Sell Currency
Date                                  
2013-12-31       100               CAD
2013-12-31       100               USD
2014-01-02       200               USD
2014-01-02       200               CAD
2014-01-03       300               CAD
2014-01-03       300               USD
2014-01-06       400               USD
2014-01-06       400               CAD

using concat使用连接

import pandas as pd


print(pd.concat(
    [df['Buy'], df['sell']], axis=1
).stack().reset_index(1, drop=True).rename(index='buy/sell')
)

output:输出:

0    100
0    100
1    200
1    200
2    300
2    300
3    400
3    400
# assuming that your data has date as index.
df.set_index('date', inplace=True)


# create a mapping to new column names
d={'Buy Currency': 'Buy/Sell Currency', 
   'Sell Currency' : 'Buy/Sell Currency',
   'Buy' : 'Buy/Sell',
   'Sell' :'Buy/Sell'
  }
df.columns=df.columns.map(d)



# stack first two columns over the next two columns
out=pd.concat([ df.iloc[:,:2],
            df.iloc[:,2:]
          ],
           ignore_index=True
          )
out
    Buy/Sell    Buy/Sell Currency
0   100     CAD
1   200     USD
2   300     CAD
3   400     USD
4   100     USD
5   200     CAD
6   300     USD
7   400     CAD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用另一个 pandas 数据框的列填充 na 值,但使用列索引,而不是名称 - Fill na values in one pandas dataframe's column using another, but using column indices, not names 按另一列分组并提取 Pandas 中一列的最高值 - Group by another column and extract top values of one column in Pandas 指数与在另一个中使用一个列表索引不同步 - Indices are not synchronous of using one lists indices in another 问题围绕堆叠的子图以虚线堆叠在彼此之上 - Issue surrounding stacked subplots in dash stacking on top of one another 如何使用pandas基于另一列查找数量前3个值 - How to find top 3 values in amount, based on another column by using pandas 使用带索引的字典向 Pandas 数据框添加一列 - Adding a column to pandas dataframe using dictionary with indices 使用一个 Pandas 数据框填充另一个 Pandas 数据框的新列 - Using one pandas dataframe to populate new column in another pandas dataframe 通过另一列中给出的索引选择一个 Pandas DataFrame 列 - Select a pandas DataFrame column by indices given in another column 使用熊猫中的另一列替换一列中的值的有效方法 - Efficient way to replace values in one column using another column in pandas 使用 pandas 将一列中的数据类别与另一列进行比较 - comparing categories of data from one column to another column using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM