[英]How to compare two columns of strings in python?
我的 CSV 文件包含 20 列,我只需要获取与我的研究相关的那些地址的数据,因此我将包含所有地址的列与仅包含特定地址的列进行比较。
我收到“关键错误”,说索引 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)
考虑使用内置函数.isin()
。
例如:
s = pd.Series(['a','b','c', 'b','c','a','b'])
所以现在 s 看起来像:
0 a
1 b
2 c
3 b
4 c
5 a
6 b
假设您只想将 s 所在的行保留在较小的系列中:
smol = pd.Series(['a','b'])
s[s.isin(smol)]
输出:
0 a
1 b
3 b
5 a
6 b
对于您的特定用例,您可能想要
data = data[data['selected_city'].isin(data['Charge_Point_City'])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.