熊猫提取信息



这是我的熊猫框架:

istat    cap               Comune
0       1001  10011               AGLIE'
1       1002  10060              AIRASCA
2       1003  10070         ALA DI STURA

我想重现等效的 SQL 查询:

Select cap
from DataFrame
where Comune = 'AIRASCA'

获得:

cap
10060

我试图用dataframe.loc()实现这一目标,但我无法检索我需要的东西。

这是我的Python代码:

import pandas as pd
from lxml import etree
from pykml import parser
def to_upper(l):
return l.upper()

kml_file_path = '../Source/Kml_Regions/Lombardia.kml'
excel_file_path = '../Source/Milk_Coverage/Milk_Milan_Coverage.xlsx'
zip_file_path = '../Source/ZipCodes/italy_cap.csv'
# Read zipcode csv
zips = pd.read_csv(zip_file_path)
zip_df = pd.DataFrame(zips, columns=['cap',  'Comune']).set_index('Comune')
zips_dict = zips.apply(lambda x: x.astype(str).str.upper())
# Read excel file for coverage
df = pd.ExcelFile(excel_file_path).parse('Comuni')
x = df['City'].tolist()
cities = list(map(to_upper, x))
#-----------------------------------------------------------------------------------------------#
# Check uncovered
# parse the input file into an object tree
with open(kml_file_path) as f:
tree = parser.parse(f)
# get a reference to the "Document.Folder" node
uncovered = tree.getroot().Document.Folder
# iterate through all "Document.Folder.Placemark" nodes and find and remove all nodes
# which contain child node "name" with content "ZONE"
for pm in uncovered.Placemark:
if pm.name in cities:
parent = pm.getparent()
parent.remove(pm)
else:
pass
# convert the object tree into a string and write it into an output file
with open('../Output/Uncovered_Milkman_LO.kml', 'w') as output:
output.write(etree.tostring(uncovered, pretty_print=True))
#---------------------------------------------------------------------------------------------#
# Check covered
with open(kml_file_path) as f:
tree = parser.parse(f)
covered = tree.getroot().Document.Folder
for pmC in covered.Placemark:
if pmC.name in cities:
pass
else:
parentCovered = pmC.getparent()
parentCovered.remove(pmC)
# convert the object tree into a string and write it into an output file
with open('../Output/Covered_Milkman_LO.kml', 'w') as outputs:
outputs.write(etree.tostring(covered, pretty_print=True))
# Writing CAP
with open('../Output/Covered_Milkman_LO.kml', 'r') as f:
in_file = f.readlines()  # in_file is now a list of lines
# Now we start building our output
out_file = []
cap = ''
#for line in in_file:
#    out_file.append(line)  # copy each line, one by one
# Iterate through dictionary which is a list transforming it in a itemable object
for city in covered.Placemark:
print zips_dict.loc[city.name, ['Comune']]

我无法理解python给我的错误,我做错了什么?从技术上讲,我可以通过在熊猫中找到值来寻找密钥,正确吗?


我认为与可能的重复问题不同,因为我要求检索单个值而不是列。

ksooklall 的答案应该可以正常工作,但是(除非我记错了(在 pandas 中使用背靠背括号有点失礼 - 它比使用 loc 慢一点,并且在使用具有许多调用的较大数据帧时实际上很重要。

像这样使用 loc 应该可以正常工作:

df.loc[df['Comune'] == 'AIRASCA', 'cap']

试试这个:

cap = df[df['Comune'] == 'AIRASCA']['cap']

您可以使用eq

前任:

import pandas as pd
df = pd.DataFrame({"istat": [1001, 1002, 1003], "cap": [10011, 10060, 10070 ], "Comune": ['AGLIE', 'AIRASCA', 'ALA DI STURA']})
print( df.loc[df["Comune"].eq('AIRASCA'), "cap"] )

输出:

1    10060
Name: cap, dtype: int64

最新更新