英文:
merge 2 columns into one within same df, eliminating "0" values
问题
Column 1 (Year1): 2019, 2018,    0,    0, 2017, 2010
Column 2 (Year2): 2019, 2017, 2013, 2011,    0, 2010
Expected output:
Column 3: 2019, 2018, 2013, 2011, 2017, 2010
Here is what I tried:
df['Column3'] = df['Column1'].replace(0, df['Column2']).astype(int)
英文:
I want to merge 2 columns into a new one, keeping all years from column 1, and replacing "0"s in column 1 for years in column 2:
Column 1 (Year1): 2019, 2018,    0,    0, 2017, 2010
Column 2 (Year2): 2019, 2017, 2013, 2011,    0, 2010
Expected output:
Column 3: 2019, 2018, 2013, 2011, 2017, 2010
Here is what I tried:
df['Column3']= df.pop('Column1').fillna(df.pop('Column2')).astype(int)
But given I have zeros "0"s in column 1, they are not being replaced.
答案1
得分: 2
以下是使用combine_first的一种方法:
import pandas as pd
df = pd.DataFrame({'col1': [2019, 2018, 0, 0, 2017, 2010], 'col2': [2019, 2017, 2013, 2011, 0, 2010]})
# 将 0 设置为 NA
df['col1'] = df['col1'].apply(lambda x: None if x == 0 else x)
# 创建新列
df['col3'] = df['col1'].combine_first(df['col2']).astype(int)
你也可以使用 apply:
df['col3'] = (df
               .apply(lambda x: x['col1'] if x['col1'] != 0 else x['col2'], 
               axis=1)
英文:
Here's a way to do using combine_first:
df = pd.DataFrame({'col1': [2019, 2018, 0, 0, 2017, 2010], 'col2': [2019, 2017, 2013, 2011, 0, 2010]})
# set 0 as NA
df['col1'] = df['col1'].apply(lambda x: None if x == 0 else x)
# create new column
df['col3'] = df['col1'].combine_first(df['col2']).astype(int)
You can also use apply
df['col3'] = (df
               .apply(lambda x: x['col1'] if x['col1'] != 0 else x['col2'], 
               axis=1)
答案2
得分: 2
Sure, here's the translated code part:
尝试:
df['y3'] = df['y1'].mask(df['y1'] == 0, df['y2'])
     y1    y2    y3
0  2019  2019  2019
1  2018  2017  2018
2     0  2013  2013
3     0  2011  2011
4  2017     0  2017
5  2010  2010  2010
英文:
Try:
df['y3'] = df['y1'].mask(df['y1'] == 0, df['y2'])
     y1    y2    y3
0  2019  2019  2019
1  2018  2017  2018
2     0  2013  2013
3     0  2011  2011
4  2017     0  2017
5  2010  2010  2010
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论