熊猫提取信息[重复]
Posted
技术标签:
【中文标题】熊猫提取信息[重复]【英文标题】:Pandas extract informations [duplicate] 【发布时间】:2019-01-26 15:30:08 【问题描述】:这是我的熊猫框架:
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 给我的错误,我做错了什么?从技术上讲,我可以通过在 pandas 中查找值来查找密钥,对吗?
我认为这与可能的重复问题不相似,因为我要求检索单个值而不是列。
【问题讨论】:
【参考方案1】:ksooklall 的答案应该可以正常工作,但是(除非我记错了)在 pandas 中使用背靠背括号有点失礼 - 它比使用 loc 慢一点,并且在使用具有许多调用的较大数据帧时实际上很重要.
像这样使用 loc 应该可以正常工作:
df.loc[df['Comune'] == 'AIRASCA', 'cap']
【讨论】:
太棒了!这似乎工作得很好!【参考方案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
【讨论】:
【参考方案3】:试试这个:
cap = df[df['Comune'] == 'AIRASCA']['cap']
【讨论】:
以上是关于熊猫提取信息[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何从熊猫框架中的特定列中提取numpy数组并将它们堆叠为单个numpy数组[重复]