簡體   English   中英

Python / Pandas-使用年份列名稱動態計算數據框中的年比率

[英]Python / Pandas - Dynamically calculate yearly ratios in a dataframe with year column names

我有這個數據框:

df:

     Co_Name  . 2014 Revenues . Address . 2012 Profits . 2014 Profits...
1 .  Apple      1231            Gjud St   20             23          ...
2 .  Orange     84894           Uinjs St  712            313         ...
3 .  Squirrel   9192            Iusaa St  4312           123         ...
...

例如:“ 2014年收入”代表某家公司在2014年的收入。

我需要計算不同的比率,使方程式具有同一年的值。 例如,我需要計算2014年的利潤率,也就是說:

df['2014 ProfitMg'] = df['2014 Profits']/df['2014 Revenues']

但是,我有很多年和很多比例要從這個數據框(巨大的數據框)中取出,所以我想以一種動態的,Python的方式做到這一點。 我想說:“ Python先生,請為以相同的4個字符的字符串開頭的名為'Profits'和'Revenues'的列計算利潤/收入”或類似的內容。

它看起來應該像這樣:

     Co_Name  . 2014 Revenues . Address . 2012 Profits . 2014 Profits . 2014 ProfitMg  ...
1 .  Apple      1231            Gjud St   20             23             0.019
2 .  Orange     84894           Uinjs St  712            313            0.008
3 .  Squirrel   9192            Iusaa St  4312           123            0.0133
...

有人可以動態地代替df['2014 ProfitMg'] = df['2014 Profits']/df['2014 Revenues']嗎?

當然,您可以找到匹配的列並將公式應用於它們:

import re
years = [re.findall(r"(\d{4})\sRevenues", col) for col in df.columns]
for year in years:
    if year:
        df['{} ProfitMg'.format(year[0])] = df['{} Profits'.format(year[0])]\
                                       / df['{} Revenues'.format(year[0])]

該解決方案假定每個“收入”列都有一個匹配的“利潤”列。 如果不是,則獲取“利潤”年份的集合和“收入”年份的集合,並求交。

import pandas as pd 
import numpy as np 

# create some data
profit_nm = ['. '+str(i)+' Profits' for i in range(1951,2051)]
revenue_nm = ['. '+str(i)+' Revenues' for i in range(1951,2051)]
column_nm = profit_nm+revenue_nm
column_nm.sort()
data = np.asarray(np.random.randint(100,1000,size=(1000,200)))
df = pd.DataFrame(data,columns=column_nm)

# function that will return the ratios
def func(pd_series):
    year = pd_series.name[:6]
    ret =  df.loc[:,year+' Profits']/df.loc[:,year+' Revenues']
    return ret

# extract names of profit columns from dataframe
profit_cols = [i for i in df.columns.tolist() if i.find(' Profits')!=-1]
#  get ratios and store in df
df2 = df.loc[:,profit_cols].apply(func, axis=0)
# change column names before joining as column names are same in df and df2
df2.columns = [year[:6]+' PftPct' for year in profit_cols]
df = df.join(df2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM