熊猫提取信息

时间:2018-08-20 13:28:37

标签: python python-2.7 pandas dataframe data-science

这是我的熊猫框架:

       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给我的错误,我在做什么错?从技术上讲,我可以通过在熊猫中找到一个值来寻找钥匙,这是正确的吗?


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

3 个答案:

答案 0 :(得分:4)

ksooklall的答案应该可以正常工作,但是(除非我记错了),在熊猫中使用背对背括号有点虚假-它比使用loc慢一些,并且在使用带有多个调用的较大数据帧时实际上很重要

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

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

答案 1 :(得分:2)

尝试一下:

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

答案 2 :(得分:2)

您可以使用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