1

This is my initial days working with pandas and excel.

I have two problems where I am stuck.

My excel look like this:-

Port NaN
1 OPS
2 WTH
3 PNS
4 SSM
5 RTY
6 EWR

I have to search for value '4' in Column1(I dont have column name so I may have to use df.iloc[:,0]).

Now, if I find the value '4' then I should output 'SSM'.

1)I am having trouble finding the value '4' not sure which function to use. I checked the type of the return object and got this.

column_port=df.iloc[:,0]
print(type(column_port))
<class 'pandas.core.series.Series'>

I am not sure how to search for a element in this return type.

2)If I found the value '4' , how to output the value from different column. In this case 'SSM'.

curious_21
  • 39
  • 1
  • 6

1 Answers1

0

you can try:

result = df.loc[df.iloc[:,0].eq(4)]['specify the column name here']
result = df.loc[df.iloc[:,0].eq(4)].iloc[:,'specify the column index here']
Nk03
  • 14,699
  • 2
  • 8
  • 22
  • Thanks @Nk03. It did give me better understanding and idea about the working. I have one question:- I have one more column to check. `excel.loc[column_B.eq(29) and excel["COLOR"]=='CAT6 Green']` Its giving me this error:- `ValueError: The truth value of a Series is ambiguous` – curious_21 May 25 '21 at 22:28
  • On google it says to use '&' instead of 'and'. – curious_21 May 25 '21 at 22:34
  • This worked for me! `excel.loc[column_B.eq(29) & excel["COLOR"].eq('CAT6 Green')` – curious_21 May 25 '21 at 22:41