如何在特定条件下将两列合并为第三列

我对Pandas相当陌生,我很难解决这个问题:

我有一个包含医生活动的DataFrame。

pd0.info()                                                                                                                                                                                                 
<class 'pandas.core.frame.DataFrame'>
Int64Index: 14059 entries,0 to 4418
Data columns (total 22 columns):
dossier               14059 non-null object
code_praticien        14059 non-null object
nom_praticien         14059 non-null object
code_anesthesiste     13128 non-null object
nom_anesthesiste      13128 non-null object
patient               14059 non-null object
sexe_patient          14059 non-null object
date_naiss_patient    14059 non-null datetime64[ns]
date                  14059 non-null datetime64[ns]
heure                 13842 non-null float64
ccam_ngap_diag        13852 non-null object
libelle               14059 non-null object
association           7682 non-null float64
modificateur1         11340 non-null object
modificateur2         1262 non-null object
modificateur3         8 non-null float64
modificateur4         0 non-null float64
montant_ccam          13684 non-null float64
montant_ngap          207 non-null float64
depassement           14049 non-null float64
total                 13901 non-null float64
praticien             13128 non-null object
dtypes: datetime64[ns](2),float64(8),object(12)
memory usage: 2.8+ MB

两列包含外科医生代码('code_praticien')和麻醉师代码('code_anesthesiste'):

test = pd0[['code_praticien','code_anesthesiste']]
test                                                                                                                                                                                                       
Out[65]: 
     code_praticien code_anesthesiste
0            BENY00            MORA01
1            BENY00            MORA01
2            BENY00            MORA01
3            BENY00            MORA01
4            BENY00            MORA01
...             ...               ...
4414         GAUD00            SAVO01
4415         SAVO01            SAVO01
4416         GAUD00            SAVO01
4417         GAUD00            SAVO01
4418         SAVO01            SAVO01

[14059 rows x 2 columns]

我正在尝试处理“外科医生”是麻醉师的情况(例如,疼痛控制程序)。 在这种情况下,我们有“ code_anesthesiste” NaN和“ code_praticien”,这是麻醉师的代码之一。 我创建了一个新列“ anesthesiste”,其中包含不为null时的“ code_anesthesiste”或“ code_praticien” 当'code_anesthesiste'isull()和'code_praticien'isin([[有效code_anesthesiste的列表])时。

test['anesthesiste'] = test.code_anesthesiste
test.loc[test.code_anesthesiste.isnull() & test.code_praticien.isin(['MORA01','SAVO01'])].anesthesiste = pd0.code_praticien

但是我一直收到此错误:“ ValueError:无法从重复的轴重新索引” 我用Google搜索了“重复轴”,但不知道我的错误在哪里...

我看了一下fillna()函数,但这似乎还不够,因为我不想在“麻醉剂”栏中添加外科医生的代码((有时外科医生在没有麻醉师的情况下工作,所以我有“ code_anesthesiste'NaN, 但是“ code_praticien”不是麻醉师的代码。

感谢您的帮助。

milu_yaya 回答:如何在特定条件下将两列合并为第三列

您可以在此处使用简单的应用:

df = pd.DataFrame({'code_practicien':['BENYY00','BENY00','GAUD00','SAVO01'],'code_anesthesiste':['MORA01','MORA01',np.NaN,'SAVO01']})
df['anethesite']=df.apply(lambda row: row['code_practicien'] if (pd.isnull(row['code_anesthesiste'])&(row['code_practicien'] in ['GAUD00','test'])) else row['code_anesthesiste'],axis=1)
df

用您当前的有效麻醉剂清单替换['GAUD00','test']

本文链接:https://www.f2er.com/3165734.html

大家都在问