Question: I am trying to create a new column by performing some operations on the existing columns but it is throwing a key error in my code. I tried to debug it by using df.columns and copy pasted the exact names but still I got the same error. My code is as follows: def calculate_elasticity(group): sales_change = group['Primary Sales Quantity'].pct_change() price_change = group['MRP'].pct_change() elasticity = sales_change / price_change return elasticity df['Variant-based Elasticity'] = df.groupby('Variant').transform(calculate_elasticity)  The error shown is --------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3801 try: -> 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err: 16 frames pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type() KeyError: 'Primary Sales Quantity' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err: -> 3804 raise KeyError(key) from err 3805 except TypeError: 3806 # If we have a listlike key, _check_indexing_error will raise KeyError: 'Primary Sales Quantity'  I tried to debug and following is the result of df.columns Index(['Cal. year / month', 'Material', 'Product Name', 'MRP', 'Distribution Channel (Master)', 'Unnamed: 5', 'L1 Prod Category', 'L2 Prod Brand', 'L3 Prod Sub-Category', 'State', 'Primary Actual GSV Value', 'Primary Sales Qty (CS)', 'Secondary GSV', 'Secondary sales Qty(CS)', 'Primary Volume(MT/KL)', 'Secondary Volume(MT/KL)', 'Variant', 'Weight', 'Offers', 'Primary Sales Quantity'], dtype='object') and result to print(df['Primary Sales Quantity']) is 0 155 1 16953 2 455 3 138 4 2653 ... 14147 6 14148 1 14149 8428 14150 237 14151 24 Name: Primary Sales Quantity, Length: 14152, dtype: int64  I tried to debug using the column names. I can even access the column by that name just the error is thrown in this function.

Answer: If use GroupBy.transform is not possible processing 2 columns together, need GroupBy.apply: def calculate_elasticity(group): sales_change = group['Primary Sales Quantity'].pct_change() price_change = group['MRP'].pct_change() group['Variant-based Elasticity'] = sales_change / price_change return group df = df.groupby('Variant', group_keys=False).apply(calculate_elasticity) print (df) Variant Primary Sales Quantity MRP Variant-based Elasticity 0 a 10 8 NaN 1 a 7 10 -1.200000 2 b 87 3 NaN 3 b 8 2 2.724138  Or change solution without helper function: g = df.groupby('Variant') df['Variant-based Elasticity'] = (g['Primary Sales Quantity'].pct_change() / g['MRP'].pct_change()) print (df) Variant Primary Sales Quantity MRP Variant-based Elasticity 0 a 10 8 NaN 1 a 7 10 -1.200000 2 b 87 3 NaN 3 b 8 2 2.724138  Alternative solution with helper df1 DataFrame: df1 = df.groupby('Variant')[['Primary Sales Quantity', 'MRP']].pct_change() df['Variant-based Elasticity'] = df1['Primary Sales Quantity'] / df1['MRP'] print (df) Variant Primary Sales Quantity MRP Variant-based Elasticity 0 a 10 8 NaN 1 a 7 10 -1.200000 2 b 87 3 NaN 3 b 8 2 2.724138  Sample data: df = pd.DataFrame({'Variant': ['a', 'a', 'b', 'b'], 'Primary Sales Quantity': [10, 7, 87, 8], 'MRP': [8, 10, 3, 2]})