[英]How to compare two columns of strings in python?
My CSV file contains 20 columns and I need to take data of only those addresses that are relevant to my study, so I compare the column containing all addresses to a column containing only specific address.我的 CSV 文件包含 20 列,我只需要获取与我的研究相关的那些地址的数据,因此我将包含所有地址的列与仅包含特定地址的列进行比较。
I am getting "key error' saying the index selected_city does not exist:我收到“关键错误”,说索引 selected_city 不存在:
import csv
import os
import pandas as pd
data_new = pd.read_csv('file1.csv', encoding= "ISO-8859–1")
print(data_new)
for i in rows:
if str(data.loc['selected_city'] == data.loc['Charge_Point_City'])
print(data.Volume,data.Charge_Point_City)
Consider using the builtin function .isin()
.考虑使用内置函数
.isin()
。
For example:例如:
s = pd.Series(['a','b','c', 'b','c','a','b'])
So now s looks like:所以现在 s 看起来像:
0 a
1 b
2 c
3 b
4 c
5 a
6 b
Say you only want to keep the rows where s is in a smaller series:假设您只想将 s 所在的行保留在较小的系列中:
smol = pd.Series(['a','b'])
s[s.isin(smol)]
Output:输出:
0 a
1 b
3 b
5 a
6 b
For your specific use case, you probably want对于您的特定用例,您可能想要
data = data[data['selected_city'].isin(data['Charge_Point_City'])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.