猿问

在两个 pandas 列之间交换值

如何根据定义的条件交换数据框中的值?


鉴于:


DF[['Exchange','predictions']]


Exchange    predictions

0   PINK    <UNK>

1   PINK    <UNK>

2   PINK    <UNK>

3   PINK    <UNK>

4   PINK    <UNK>

... ... ...

490541  NASDAQ  PINK

490542  NaN PINK

490543  NASDAQ  PINK

490544  NaN PINK

490545  NASDAQ  PINK

仅当 Exchange 值为 NaN 并且预测值不是 < UNK > 时,我才希望将 Exchange 替换为预测中的值。


慕斯709654
浏览 119回答 1
1回答

幕布斯6054654

让我们尝试fillna部分条件Seriesdf.Exchange.fillna(df.loc[df['predictions'].ne('<UNK>'), 'predictions'], inplace=True)dfOut[210]:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Exchange predictions0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PINK&nbsp; &nbsp; &nbsp; &nbsp;<UNK>1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PINK&nbsp; &nbsp; &nbsp; &nbsp;<UNK>2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PINK&nbsp; &nbsp; &nbsp; &nbsp;<UNK>3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PINK&nbsp; &nbsp; &nbsp; &nbsp;<UNK>4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PINK&nbsp; &nbsp; &nbsp; &nbsp;<UNK>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...490541&nbsp; &nbsp;NASDAQ&nbsp; &nbsp; &nbsp; &nbsp; PINK490542&nbsp; &nbsp; &nbsp;PINK&nbsp; &nbsp; &nbsp; &nbsp; PINK490543&nbsp; &nbsp;NASDAQ&nbsp; &nbsp; &nbsp; &nbsp; PINK490544&nbsp; &nbsp; &nbsp;PINK&nbsp; &nbsp; &nbsp; &nbsp; PINK490545&nbsp; &nbsp;NASDAQ&nbsp; &nbsp; &nbsp; &nbsp; PINK
随时随地看视频慕课网APP

相关分类

Python
我要回答