0

I have Id column , name and age I want to get the ID value from ID column of the NA value in Name column , how can I do that with python ? I was thinking of using a for loop to get the index of NA then find index of ID but I thought maybe there is an easier way

A = [["1091",'tom', 25], ["192",'krish', 30],
       ["138",'nick', 26], ["120","",20],["32",'juli', 22],["99","",19]]

B = [["1091","ALEX"], ["192","SAM"],
       ["138",'JACK'], ["120","MAT"],["99","LISA"]]
       
dfA = pd.DataFrame(A, columns =['ID','Name', 'Age'])
dfB = pd.DataFrame(B, columns =['ID','Name'])
dfA = dfA.replace("",np.nan, regex=True)
print(dfA)

output dfA

  ID     Name     Age
0  1091   tom    25
1  192  krish    30
2  138   nick    26
3  120    NaN    20
4  32    juli    22
5  99    NaN    19

blackCAt
  • 11
  • 5
  • Do you mean like `dfA['Name'] = dfA['Name'].fillna(dfA['ID'].map(dfB.set_index('ID')['Name']))`? Like [this answer](https://stackoverflow.com/a/60116654/15497888) by [ansev](https://stackoverflow.com/users/11884237/ansev) – Henry Ecker Nov 14 '21 at 08:10
  • Where do these ID numbers come from? In your provided DataFrames the ids are `[1091, 192, 138, 120, 32, 99]`. – Henry Ecker Nov 14 '21 at 08:33
  • @HenryEcker sorry I forgot to edit the df (I'm editing it now) – blackCAt Nov 14 '21 at 08:34
  • for example row 4 & 6 in column name have NA values they're associated with ID number 4 & 6 I want to get these values (120&99) – blackCAt Nov 14 '21 at 08:36
  • So just like `print(dfA.loc[dfA['Name'].isna(), 'ID'])`? – Henry Ecker Nov 14 '21 at 08:40

1 Answers1

0

Not sure how you want the output printed but you can use np.where to find the indexes as seen below.

select_indices = list(np.where(dfA.loc[:,"Name"].isnull()))
for item in select_indices[0]: #select_indices is an array, you need to access the first element to print the indexes
    print(item)

EDIT

If you want the table rows where the NaN values are use this. You can also specify just the column "ID" instead of ":" if you only want that column.

select_indices = list(np.where(dfA.loc[:,"Name"].isnull()))[0]
dfA.loc[select_indices,:] #specify column here

If you want literally just to print the 120 and 99 use:

select_indices = list(np.where(dfA.loc[:,"Name"].isnull()))[0]
for row in dfA.loc[select_indices,"ID"].iteritems():
    print(row[1])
Kirsten_J
  • 96
  • 2
  • for example row 4 & 6 in column name have NA values they're associated with ID number 4 & 6 I want to get these values (120&99) – blackCAt Nov 14 '21 at 08:36