简体   繁体   English

将两列相乘 pandas MultiIndex dataframe

[英]Multiply two columns in a pandas MultiIndex dataframe

I have never used multiIndex pandas dataframes, and the Python API for CalcBench, a financial program I use, returned one when I typed the following code:我从未使用过 multiIndex pandas 数据帧,而 Python API 用于我使用的财务程序 CalcBench,当我键入以下代码时返回一个:

dataItems = ["ROE", "StockholdersEquity", "SharesOutstandingEndOfPeriod", "EndOfPeriodStockPrice"]             
data = cb.normalized_data(tickers, dataItems, start_year=2021, end_year=2021, period_type='annual')

If I try to explore my dataframe by typing data.info() , I get:如果我尝试通过键入data.info()来探索我的 dataframe,我得到:

<class 'pandas.core.frame.DataFrame'>
PeriodIndex: 1 entries, 2021 to 2021
Freq: A-DEC
Columns: 1800 entries, ('EndOfPeriodStockPrice', 'A') to ('StockholdersEquity', 'ZTS')
dtypes: float64(1800)

Typing data.columns() gives me:输入data.columns()给我:

MultiIndex([('EndOfPeriodStockPrice',    'A'),
            ('EndOfPeriodStockPrice',  'AAL'),
            ('EndOfPeriodStockPrice',  'AAP'),
            ('EndOfPeriodStockPrice', 'AAPL'),
            ('EndOfPeriodStockPrice', 'ABBV'),
            ('EndOfPeriodStockPrice',  'ABC'),
            ('EndOfPeriodStockPrice',  'ABT'),
            ('EndOfPeriodStockPrice',  'ACN'),
            ('EndOfPeriodStockPrice', 'ADBE'),
            ('EndOfPeriodStockPrice',  'ADI'),
            ...
            (   'StockholdersEquity', 'WYNN'),
            (   'StockholdersEquity',  'XEL'),
            (   'StockholdersEquity',  'XOM'),
            (   'StockholdersEquity', 'XRAY'),
            (   'StockholdersEquity',  'XYL'),
            (   'StockholdersEquity',  'YUM'),
            (   'StockholdersEquity',  'ZBH'),
            (   'StockholdersEquity', 'ZBRA'),
            (   'StockholdersEquity', 'ZION'),
            (   'StockholdersEquity',  'ZTS')],
           names=['metric', 'ticker'], length=1800)

I would like to create a new metric MarketCapAtEndOfPeriod in this dataframe for each firm in the year 2021 by multiplying the corresponding entries for EndofPeriodStockPrice and SharesOutstandingEndOfPeriod .我想通过将EndofPeriodStockPriceSharesOutstandingEndOfPeriod的相应条目相乘,在 dataframe 中为 2021 年的每家公司创建一个新指标MarketCapAtEndOfPeriod

My efforts to do so have gone nowhere, and searching StackOverflow has not helped me solve my problem.我为此所做的努力没有任何结果,搜索 StackOverflow 也没有帮助我解决我的问题。 Loosely speaking, I'm looking to write something like:松散地说,我想写这样的东西:

data["MarketCapAtEndOfPeriod"] = data["EndofPeriodStockPrice"] * data["SharesOutstandingEndOfPeriod"]
market_cap = data.EndOfPeriodStockPrice * data.SharesOutstandingEndOfPeriod
market_cap_df = pd.concat(
    {"MarketCap": market_cap},
    names=["metric"],
    axis=1,
)

data = data.join(market_cap_df)

multiplying the corresponding entries for EndofPeriodStockPrice and SharesOutstandingEndOfPeriodEndofPeriodStockPriceSharesOutstandingEndOfPeriod的相应条目相乘

Use DataFrame.xs to access MultiIndex cross-sections by specifying the label and an optional axis/level.使用DataFrame.xs通过指定 label 和可选的轴/水平来访问 MultiIndex 横截面。

  • In your case, the target labels are in the first level (default), so you can leave out level=0 and just specify axis=1 for columns.在您的情况下,目标标签位于第一级(默认),因此您可以省略level=0并为列指定axis=1
  • To assign the output as new columns, reconstruct the MultiIndex with MultiIndex.from_product before rejoining with the original dataframe.要将 output 分配为新列,请在重新加入原始MultiIndex.from_product之前使用 MultiIndex.from_product 重建 MultiIndex。
key1 = 'EndofPeriodStockPrice'
key2 = 'SharesOutstandingEndOfPeriod'
new = 'MarketCapAtEndOfPeriod'

prod = data.xs(key1, axis=1) * data.xs(key2, axis=1)

prod.columns = pd.MultiIndex.from_product([[new], [key1, key2]])
# MultiIndex([('MarketCapAtEndOfPeriod', 'EndofPeriodStockPrice'),
#             ('MarketCapAtEndOfPeriod', 'SharesOutstandingEndOfPeriod')])

df = df.join(prod)

If hypothetically you wanted to divide corresponding AAPL and ABC entries, those are in the second column level, so specify both axis=1 and level=1 :如果假设您想划分相应的AAPLABC条目,则它们位于第二列级别,因此同时指定axis=1level=1

data.xs('AAPL', axis=1, level=1) / data.xs('ABC', axis=1, level=1)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM