简体   繁体   English

如果其他列值相等,则熊猫返回列值

[英]Pandas return column value if other column value is equal

so recentyl im trying to get data from CSV file using python and pandas. 因此,最近我尝试使用python和pandas从CSV文件中获取数据。 Code should return or print data from colum 1 if data from column 2 is equal to some string. 如果列2中的数据等于某个字符串,则代码应返回或打印列1中的数据。

    import pandas as pd
df = pd.read_csv('alerts.csv', sep=';', encoding='latin1')

print(df[['color']['item']].loc[['color']=='red'])

but its seems not working with strings ? 但是它似乎不适用于字符串吗?

you are not using .loc correctly 您没有正确使用.loc

.loc need an indexer, and columns such has .loc需要一个索引器,而这样的列具有

indexer = df[df['color']=='red'].index
print(df.loc[indexer,'item'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM