Home > Back-end >  How get values of a column using other a value from other column in Pandas
How get values of a column using other a value from other column in Pandas

Time:09-25

Hello I have the following list of dataframes generate by:

Code:

def frequency_tables(headers, target, data):
    freq = []
    for i in headers:
        freq_table = data.groupby([i, target]).size().reset_index(name="Time")
        freq.append(freq_table)
    return freq

Data frame:

outlook   play   Time
overcast  yes    4
rainy     no     2
rainy     yes    3
sunny     no     3
sunny     yes    2

I'd like to know how to get the values of Time using the values from outlook and do some calculus, for example:

rainy|yes   rainy|no = 5

CodePudding user response:

A quick and simple way to select rows from a value is to use the dataframe's loc method, such as this:

rainy_days = df.loc[df['overlook'] == 'rainy']

It should create a new dataframe with only rainy day rows, and from that you can do the calculations normally. If you'd like to use more than one condition, you can stack the conditions like this:

rainy_days_with_play = df.loc[(df['overlook'] == 'rainy') & (df['play'] == 'yes'])
  • Related