我有以下数据框:
# EPISODE PROGRAM TITLE IS_EPIS IS_MOVIE IS_SHOW
1 E_N1 P_N1 T1 1 0 0
2 E_N2 P_N2 T2 0 0 1
3 E_N3 P_N3 T3 0 1 0
我试图获得新的专栏为:
# EPISODE PROGRAM TITLE IS_EPIS IS_MOVIE IS_SHOW TITLE EPISODE_TITLE
1 E_N1 P_N1 T1 1 0 0 NA E_N1
2 E_N2 P_N2 T2 0 0 1 T2 NA
3 E_N3 P_N3 T3 0 1 0 P_N3 NA
通过:
df['episode_title'] = df[(df.IS_EPIS == 1)].episode #GOOD
df['title'] = df[(df.IS_MOVIE == 1)].program | df[(df.IS_SHOW == 1)].title #FAIL
最佳答案
您可以使用loc
:
df.loc[df.IS_MOVIE == 1 ,'title'] = df.PROGRAM
df.loc[df.IS_SHOW == 1, 'title'] = df.TITLE
df.loc[df.IS_EPIS == 1, 'episode_title'] = df.EPISODE
print df
EPISODE PROGRAM TITLE IS_EPIS IS_MOVIE IS_SHOW title episode_title
#
1 E_N1 P_N1 T1 1 0 0 NaN E_N1
2 E_N2 P_N2 T2 0 0 1 T2 NaN
3 E_N3 P_N3 T3 0 1 0 P_N3 NaN
关于python - 列中的Concat值取决于特定条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34208273/