Reputation: 661
I am trying to work out how to fill some rows with text based on other columns but am struggling to figure it out. Below is an example of a subset of data I have in a pandas data frame
.
Group Name_X Name_Y Code Group_Code Group_Correct
0 Group_A Company_PJ Company_PJ M001 1-00-002 Group_A
1 Group_A_Ltd Company_PJ NaN NaN NaN NaN
2 Group_B Company_XYZ NaN NaN NaN NaN
3 Group_GTY Company_R Company_R M020 1-00-033 Group_GTY
4 Group_A Company_PJ NaN NaN NaN NaN
5 Group_BGG Company_VV Company_VV M023 1-00-233 Group_BGG
6 Group_B Company_XYZ NaN NaN NaN NaN
7 Group_B Company_XYZ Company_XYZ M003 1-00-003 Group_B
8 Group_B_Limited Company_XYZ NaN NaN NaN NaN
9 Group_B Company_DEF Company_DEF M004 1-00-006 Group_B
10 Group_B+ Company_DEF NaN NaN NaN NaN
And what I would like is to get something like the below. The text shown in red are the text that needs to be filled or changed based on conditions of text in other rows.
What's the easiest way I could go about doing it in python
.
Upvotes: 1
Views: 1995
Reputation: 323396
Using transform
first
df=df.groupby('Name_x').transform('first')
If you have some column different for each row
l=['name1','name2'] # column different from each row
pd.concat([df.drop(l,1).groupby('Name_x').transform('first'),df[l]],axis=1).reindex(columns=df.columns)
Update :
pd.concat([df.drop(l,1).groupby('Name_x').transform('first'),df[l.append('Name_x')]],axis=1).reindex(columns=df.columns)
Update2 :
pd.concat([df.drop(l,1).groupby('Name_x').transform('first'),df[l],df[['Name_x']]],axis=1).reindex(columns=df.columns)
Upvotes: 2