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