简体   繁体   中英

write a python function using ```def``` from 3 pre-existing columns in a dataframe; columns 1 and 2 as inputs = column 3 as output

My dataframe looks like this. 3 columns. All I want to do is write a FUNCTION that, when the first two columns are inputs, the corresponding third column (GHG intensity) is the output. I want to be able to input any property name and year and achieve the corresponding GHG intensity value. I cannot stress enough that this has to be written as a function using def . Please help!

                             Property Name  Data Year  \
467                             GALLERY 37       2018   
477                        Navy Pier, Inc.       2016   
1057                            GALLERY 37       2015   
1491                       Navy Pier, Inc.       2015   
1576                            GALLERY 37       2016   
2469                   The Chicago Theatre       2016   
3581                       Navy Pier, Inc.       2014   
4060                        Ida Noyes Hall       2015   
4231               Chicago Cultural Center       2015   
4501                            GALLERY 37       2017   
5303                         Harpo Studios       2015   
5450                   The Chicago Theatre       2015   
5556               Chicago Cultural Center       2016   
6275   MARTIN LUTHER KING COMMUNITY CENTER       2015   
6409   MARTIN LUTHER KING COMMUNITY CENTER       2018   
6665                        Ida Noyes Hall       2017   
7621                        Ida Noyes Hall       2018   
7668   MARTIN LUTHER KING COMMUNITY CENTER       2017   
7792                   The Chicago Theatre       2018   
7819                        Ida Noyes Hall       2016   
8664   MARTIN LUTHER KING COMMUNITY CENTER       2016   
8701                   The Chicago Theatre       2017   
9575               Chicago Cultural Center       2017   
10066              Chicago Cultural Center       2018   

       GHG Intensity (kg CO2e/sq ft)  
467                             7.50  
477                            22.50  
1057                            8.30  
1491                           23.30  
1576                            7.40  
2469                            4.50  
3581                           17.68  
4060                           11.20  
4231                           13.70  
4501                            7.90  
5303                           18.70  
5450                             NaN  
5556                           10.30  
6275                           14.10  
6409                           12.70  
6665                            8.30  
7621                            8.40  
7668                           12.10  
7792                            4.40  
7819                           10.20  
8664                           12.90  
8701                            4.40  
9575                            9.30  
10066                           7.50 

Here is an example, with aa different data frame to test:

import pandas as pd

df = pd.DataFrame(data={'x': [3, 5], 'y': [4, 12]})

def func(df, arg1, arg2, arg3):
    ''' agr1 and arg2 are input columns; arg3 is output column.'''
    df = df.copy()
    df[arg3] = df[arg1] ** 2 + df[arg2] ** 2
    return df

Results are:

print(func(df, 'x', 'y', 'z'))

   x   y    z
0  3   4   25
1  5  12  169

You can try this code

def GHG_Intensity(PropertyName, Year):
    Intensity = df[(df['Property Name']==PropertyName) & (df['Data Year']==Year)]['GHG Intensity (kg CO2e/sq ft)'].to_list()
    return Intensity[0] if len(Intensity) else 'GHG Intensity Not Available'

print(GHG_Intensity('Navy Pier, Inc.', 2016))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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