[英]Search for a value in a column and return value from the same row but different column using Python and pandas
This is my initial days working with pandas and excel.这是我最初使用 pandas 和 excel 的日子。
I have two problems where I am stuck.我有两个问题,我被卡住了。
My excel look like this:-我的 excel 看起来像这样:-
Port港口 | NaN钠 |
---|---|
1 1 | OPS运维 |
2 2 | WTH WTH |
3 3 | PNS PNS |
4 4 | SSM SSM |
5 5 | RTY RTY |
6 6 | EWR EWR |
I have to search for value '4' in Column1(I dont have column name so I may have to use df.iloc[:,0]).我必须在 Column1 中搜索值“4”(我没有列名,所以我可能必须使用 df.iloc[:,0])。
Now, if I find the value '4' then I should output 'SSM'.现在,如果我找到值“4”,那么我应该 output 'SSM'。
1)I am having trouble finding the value '4' not sure which function to use. 1)我无法找到值“4”,不确定要使用哪个 function。 I checked the type of the return object and got this.我检查了返回 object 的类型并得到了这个。
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. 2)如果我找到值'4',如何 output 来自不同列的值。 In this case 'SSM'.在本例中为“SSM”。
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.