我正在尝试找到一种更有效的方法,根据单独的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
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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