1. 程式人生 > 其它 >dataframe 針對列條件賦值

dataframe 針對列條件賦值

針對單列條件:

#常規方式
import pandas as pd

df = pd.DataFrame({'one':['a', 'a', 'b', 'c'], 'two':[3,1,2,3], 'three':['C','B','C','A']})
print(df)

df.loc[df['two']==2, 'one']='x' #修改列"one"的值,推薦使用.loc
print(df)

df.one[df.two==2]='x'
print(df)
#函式方式
def fun(x):
    if x >= 30:
        return 1
    else
: return 0 values= feature['values'].apply(lambda x: fun(x)) #若需要將改動賦值給原始的feature的列中的話,可以進行一次賦值 feature['values']=values #或者直接一次修改後賦值。 feature['values']= feature['values'].apply(lambda x: fun(x))
import numpy as np
import pandas as pd

data = {'city': ['Beijing', 'Shanghai', 'Guangzhou', '
Shenzhen', 'Hangzhou', 'Chongqing'],
    'year': [2016,2016,2015,2017,2016, 2016], 'population': [2100, 2300, 1000, 700, 500, 500]} frame = pd.DataFrame(data, columns = ['year', 'city', 'population', 'debt']) # 使用apply函式, 如果city欄位包含'ing'關鍵詞,則'判斷'這一列賦值為1,否則為0 frame['panduan'] = frame.city.apply(lambda x: 1 if 'ing' in
x else 0) print(frame)

針對多列的條件:

#常規方式
import pandas as pd

df = pd.DataFrame({'one':['a', 'a', 'b', 'c'], 'two':[3,1,2,3], 'three':['C','B','C','A']})
print(df)

df.loc[(df['two']==2) | (df['three']=='A'), 'one']='x'#推薦使用.loc
print(df)

df.loc[(df['two']==2) & (df['three']=='C'), 'one']='x'#推薦使用.loc
print(df)
import numpy as np
import pandas as pd
 
data = {'city': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hangzhou', 'Chongqing'],
       'year': [2016,2016,2015,2017,2016, 2016],
       'population': [2100, 2300, 1000, 700, 500, 500]}
frame = pd.DataFrame(data, columns = ['year', 'city', 'population', 'debt'])
 
def function(a, b):
    if 'ing' in a and b == 2016:
        return 1
    else:
        return 0

frame['test'] = frame.apply(lambda x: function(x.city, x.year), axis = 1)
print(frame)
def win_or_loss(df):
    cond_loss_1 = (df['gli_h'] < -80) & (df['sc_h'] > df['sc_g'])
    cond_loss_2 = (df['gli_g'] < -80) & (df['sc_h'] < df['sc_g'])
    cond_loss_3 = (df['gli_drew'] < -80) & (df['eur_h'] < df['eur_g']) & (df['sc_h'] < df['sc_g'])
    cond_loss_4 = (df['gli_drew'] < -80) & (df['eur_h'] > df['eur_g']) & (df['sc_h'] > df['sc_g'])
    cond_loss = cond_loss_1 | cond_loss_2 | cond_loss_3 | cond_loss_4
    #
    cond_win_1 = (df['gli_h'] < -80) & (df['sc_h'] < df['sc_g'])
    cond_win_2 = (df['gli_g'] < -80) & (df['sc_h'] > df['sc_g'])
    cond_win_3 = (df['gli_drew'] < -80) & (df['eur_h'] < df['eur_g']) & (df['sc_h'] > df['sc_g'])
    cond_win_4 = (df['gli_drew'] < -80) & (df['eur_h'] > df['eur_g']) & (df['sc_h'] < df['sc_g'])
    cond_win = cond_win_1 | cond_win_2 | cond_win_3 | cond_win_4
    #
    if cond_win:
        return 'win'
    elif cond_loss:
        return 'loss'
    else:
        return 'd'

def df_mark_win(df):
    cond_price = (df['price_h'] > 1.9) & (df['price_drew'] > 1.9) & (df['price_g'] > 1.9)
    cond_trd = (df['trade_h'] > 300000) | (df['trade_drew'] > 300000) | (df['trade_g'] > 300000)
    cond_bfidx = (df['index_h'] > 80) | (df['index_drew'] > 80) | (df['index_g'] > 80)
    cond_gli = (df['gli_h']<-80) | (df['gli_drew']<-80) | (df['gli_g']<-80)
    cond_hot = (df['hot_h'] > 80) | (df['hot_drew'] > 80) | (df['hot_g'] > 80)
    df_rst = df.loc[cond_price & cond_trd & cond_bfidx & cond_gli & cond_hot].copy()
    #用copy()避免在原df上操作避免報錯
    df_rst['result'] = df_rst.apply(lambda x: win_or_loss(x), axis=1)
    return df_rst