Merge 2 columns into 1 column

孤街醉人 提交于 2021-02-10 07:33:35

问题


I will like to merge 2 columns into 1 column and remove nan.

I have this data:

     Name       A       B   
    Pikachu   2007    nan
    Pikachu   nan     2008
    Raichu    2007    nan
    Mew       nan     2018

Expected Result:

     Name     Year   
    Pikachu   2007   
    Pikachu   2008   
    Raichu    2007   
    Mew       2008 

Code I tried:

df['Year']= df['A','B'].astype(str).apply(''.join,1)

But my result is this:

 Name     Year   
Pikachu   2007nan   
Pikachu   nan2008   
Raichu    2007nan   
Mew       nan2008

回答1:


Use Series.fillna with DataFrame.pop for extract columns and last convert to integers:

df['Year']= df.pop('A').fillna(df.pop('B')).astype(int)

#if possible some missing values in Year column
#df['Year']= df.pop('A').fillna(df.pop('B')).astype('Int64')
print (df)
      Name  Year
0  Pikachu  2007
1  Pikachu  2008
2   Raichu  2007
3      Mew  2018



回答2:


Could you please try following.

df['Year']=df['A'].combine_first(df['B'])
df

Output will be as follows.

     Name    A       B      Year
0   Pikachu 2007.0  NaN     2007.0
1   Pikachu NaN     2008.0  2008.0
2   Raichu  2007.0  NaN     2007.0
3   Mew     NaN     2018.0  2018.0


To get only Name and year columns in a new data frame try following.

df['Year']=df['A'].combine_first(df['B'])
df1=df[['Name','Year']]
df1



回答3:


df = df.fillna(0)
df["Year"] = df["A"] + df["B"]
df = df[['Name','Year']]



回答4:


numpy.where could be usefull

df["A"] = np.where(df["A"].isna(), df["B"], df["A"]).astype("int")
df = df.drop("B", axis=1)
print(df)
    Name    Year
0   Pikachu 2007
1   Pikachu 2008
2   Raichu  2007
3   Mew     2018


来源:https://stackoverflow.com/questions/58537651/merge-2-columns-into-1-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!