合并同一数据框中的两列,消除 “0” 值。

huangapple go评论194阅读模式
英文:

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:

  1. 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:

  1. 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的一种方法:

  1. import pandas as pd
  2. df = pd.DataFrame({'col1': [2019, 2018, 0, 0, 2017, 2010], 'col2': [2019, 2017, 2013, 2011, 0, 2010]})
  3. # 将 0 设置为 NA
  4. df['col1'] = df['col1'].apply(lambda x: None if x == 0 else x)
  5. # 创建新列
  6. df['col3'] = df['col1'].combine_first(df['col2']).astype(int)

你也可以使用 apply

  1. df['col3'] = (df
  2. .apply(lambda x: x['col1'] if x['col1'] != 0 else x['col2'],
  3. axis=1)
英文:

Here's a way to do using combine_first:

  1. df = pd.DataFrame({'col1': [2019, 2018, 0, 0, 2017, 2010], 'col2': [2019, 2017, 2013, 2011, 0, 2010]})
  2. # set 0 as NA
  3. df['col1'] = df['col1'].apply(lambda x: None if x == 0 else x)
  4. # create new column
  5. df['col3'] = df['col1'].combine_first(df['col2']).astype(int)

You can also use apply

  1. df['col3'] = (df
  2. .apply(lambda x: x['col1'] if x['col1'] != 0 else x['col2'],
  3. axis=1)

答案2

得分: 2

Sure, here's the translated code part:

尝试:

  1. df['y3'] = df['y1'].mask(df['y1'] == 0, df['y2'])
  1. y1 y2 y3
  2. 0 2019 2019 2019
  3. 1 2018 2017 2018
  4. 2 0 2013 2013
  5. 3 0 2011 2011
  6. 4 2017 0 2017
  7. 5 2010 2010 2010
英文:

Try:

df['y3'] = df['y1'].mask(df['y1'] == 0, df['y2'])

  1. y1 y2 y3
  2. 0 2019 2019 2019
  3. 1 2018 2017 2018
  4. 2 0 2013 2013
  5. 3 0 2011 2011
  6. 4 2017 0 2017
  7. 5 2010 2010 2010

huangapple
  • 本文由 发表于 2020年1月4日 00:32:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定