根据单独列中的值分配新列

Assign new Column based off values in separate Column

我正在尝试找到一种更有效的方法来根据单独的 Column 中的值将值分配给 Column。对于下面的 df,我想将 0 分配给工作日,将 1 分配给周末。

这是我的尝试:

import pandas as pd
import numpy as np

d = ({             
   'Day' : ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],                                                                                                                                                 
    })

df = pd.DataFrame(data=d)

df['Group'] = np.where(df['Day'] == 'Monday', 0, 1)
df['Group'] = np.where(df['Day'] == 'Tuesday', 0, 1)
df['Group'] = np.where(df['Day'] == 'Wednesday', 0, 1)
df['Group'] = np.where(df['Day'] == 'Thursday', 0, 1)
df['Group'] = np.where(df['Day'] == 'Friday', 0, 1)
df['Group'] = np.where(df['Day'] == 'Saturday', 1, 0)
df['Group'] = np.where(df['Day'] == 'Sunday', 1, 0)

Output:

         Day  Group
0     Monday      0
1    Tuesday      0
2  Wednesday      0
3   Thursday      0
4     Friday      0
5   Saturday      0
6     Sunday      1

Intended Output:

         Day  Group
0     Monday      0
1    Tuesday      0
2  Wednesday      0
3   Thursday      0
4     Friday      0
5   Saturday      1
6     Sunday      1

这是使用 df.apply

的一种方法
import pandas as pd

DAYVALUE = {'Monday':0, 'Tuesday':0, 'Wednesday':0, 'Thursday':0, 'Friday':0, 'Saturday':1, 'Sunday':1}

>>>df
         Day
0     Monday
1    Tuesday
2  Wednesday
3   Thursday
4     Friday
5   Saturday
6     Sunday

df['Group'] = df.loc[:,'Day'].apply(lambda x: DAYVALUE[x])

>>>df
         Day  Group
0     Monday      0
1    Tuesday      0
2  Wednesday      0
3   Thursday      0
4     Friday      0
5   Saturday      1
6     Sunday      1

每次 df['Group'] = np.where(...) 都是从头开始设置 Group 列的值。因此,在您的一系列陈述中,唯一真正重要的是最后一个:

df['Group'] = np.where(df['Day'] == 'Sunday', 1, 0)

要解决此问题,您可以一次设置 Group 列的所有值:

import pandas as pd
import numpy as np

d = ({             
   'Day' : ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],                                                                                                                                                 
    })

df = pd.DataFrame(data=d)
df['Group'] = np.where(df['Day'].isin(['Saturday', 'Sunday']), 1, 0)

Output:

         Day  Group
0     Monday      0
1    Tuesday      0
2  Wednesday      0
3   Thursday      0
4     Friday      0
5   Saturday      1
6     Sunday      1

你也可以这样查看

d = pd.DataFrame({             
   'Day' : ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],                                                                                                                                                 
    })


group = []

  for i, row in d.iterrows():
    if row['Day'] == 'Saturday' or row['Day'] == 'Sunday':
        group.append(0)
    else:
        group.append(1)
d['group'] =group