I have a dataframe df
as follows:
KPI Tata JSW
Gross Margin % 0.582 0.476
EBITDA Margin % 0.191 0.23
EBIT Margin % 0.145 0.183
SG&A/Revenue 0.141 0.03
COGS/Revenue 0.418 0.524
CapE/Revenue 0.0577 0.1204
ROA 0.064 0.093
ROE 0.138 0.243
Revenue/Employee $K 290.9 934.4
Inventory Turnover 2.2 3.27
AR Turnover 13.02 14.29
Tot Asset Turnover 0.68 0.74
Current Ratio 0.9 0.8
Quick Ratio 0.3 0.4
I am trying to add a column, say, scope
based on the following criterion:
if df[df['KPI'].str.contains('Margin|Revenue|ROE|ROA')].shape[0] > 0:
z = 'Max'
elif df[df['KPI'].str.contains('Quick|Current|Turnover')].shape[0] > 0:
z = 'Min'
In other words, If the field KPI
contains any word like Revenue
or Margin
then the column scope
should take Max
else Min
. Now there is an exception in KPI == COGS/Revenue
or KPI == CapEx/Revenue
. In this case the scope
should take Min
despite the string Revenue
is present.
So the resultant df
should look like below:
In order to achieve the same I am trying to apply
a function on the field KPI
.
def scope_superlative(col_name):
df_test = df[df[col_name].str.contains('Margin| Revenue|ROA|ROE')]
if df_test.shape[0] > 0:
z = 'Max'
else:
df_test = df[df[col_name].str.contains('/Revenue|Current|Quick|Turnover')] ##<-- I want to check if string 'Revenue' is in denominator.##
if df_test.shape[0] > 0:
z='Min'
return z
##Applying this function##
df['scope'] = df.KPI.apply(lambda x : scope_superlative(x))
The above code is generating an Error
as KeyError: 'Gross Margin %
If I use df['scope']=df.apply(scope_superlative('KPI'), axis=1)
I get an Error as AttributeError: 'DataFrame' object has no attribute 'Max'
.
Can anybody please help on this? Is there anything wrong in function or applying technique?