简体   繁体   中英

Looking for an easier way to enter a 0 or 1 in a pandas dataframe based on the value of another column

I am looking for a way to evaluate a value in the pandas dataframe to see if it is the max value in that column. If it is, another column would get a 1 or else it would get 0. This is what I have so far but I am thinking there must be a more pythonic way. Please note that the code for returning moving averages isn't done yet.

def period_contribution(df, column_name, start_date, 
                        end_date, expanded_stats=False):
    '''
    Creates a dataframe that shows the return of each stock in the index
    for the supplied period.
    If expanded_stats is selected, 10d, 50d, 200d moving averages are returned
    as well as binary indicator for 52 week high or low.    

    Parameters
    ----------
    df : pd.DataFrame
        Source dataframe of all data that columns will be added to.
    column_name : string
        The column from the holdings dataframe that specifies the symbol in
        yahoo format is supplied as a string
    start_date : timestamp
        beginning date to use for price retrieval
    end_date : timestamp
        end date to use for price retrieval.
    expanded_stats : Boolean, optional
        select if moving averages and 52 week high/low. 
        The default is False.

    Returns
    -------
    pd.DataFrame copy of the supplied df dataframe.

    '''
    for i in range(0, 2):
        symbol = df[column_name][i]
        df_return = DataReader(symbol, 'yahoo',
                                start_date, end_date)['Adj Close'].to_frame()
        
        df.iloc[i, -1] = (df_return.iloc[-1, 0] /
                                          df_return.iloc[0, 0] -1)
        
        if df_return.iloc[-1, 0] == df_return['Adj Close'].max():
            df.iloc[i, df.columns.get_loc('52 Wk High')] = 1
        else:
            df.iloc[i, df.columns.get_loc('52 Wk High')] = 0

    return df
import pandas as pd
df = pd.DataFrame({"val":[1,2,3,4,5,99,6,7,8,9]})

df["is_max"] = 0
df["is_max"].iloc[df["val"].argmax()] = 1

print(df)

   val  is_max
0    1       0
1    2       0
2    3       0
3    4       0
4    5       0
5   99       1
6    6       0
7    7       0
8    8       0
9    9       0

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