简体   繁体   English

使用 Python 和 pandas 在列中搜索值并返回同一行但不同列的值

[英]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.

相关问题 使用 Python Pandas 将列值与不同列进行比较,并从同一行但不同列返回值 - Compare a Column value to different columns and return a value from same row but different column using Python Pandas 如何遍历 Pandas DF 中的列以检查某个值并返回同一行但来自不同列的值? - How to iterate over a column in a Pandas DF to check for a certain value and return a value in the same row but from a different column? python 在列中搜索字符串,并从同一行返回另一个列值 - python Search a string in a column, and return another column value from that same row Pandas:如何使用其他 dataframe 的列值从 dataframe 返回具有相同行值的行? - Pandas: How to return the row from dataframe having same row values by using column value of other dataframe? Pandas:同列不同行如何填写值 - Pandas:How to fill in value from the same column but different row 搜索列,如果值不是所有数字,则将值剪切并粘贴到同一行的另一列 python pandas - Search column, if value is not all digits, then cut&paste value to another column same row python pandas 如何使用 Pandas 中的索引列从“其他”行返回值 - How to return value from "other" row using index column in Pandas Pandas 在不同的列上返回具有条件行 statemnet 的列的值 - Pandas return value of column with conditional row statemnet on a different column 如何用同一行但不同列中的值替换熊猫中的值 - How to replace value in pandas with value in the same row but in a different column 在列中查找字符串,但在同一行下的其他列中返回值 - find a string in a column but return the value in a different column under the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM