Pandas:loc的使用示例

loc 使用pandas中的一个很有用的属性。

loc是location的缩写,意为定位数据。
 
iloc是integer location的缩写,意为整数位置。iloc函数用于按位置选择数据,即可以通过行号或列号来索引数据。
 
与loc函数不同,loc函数是根据标签(label)来查找数据的,而iloc函数则是根据整数位置来查找数据的。因此,iloc函数中的“i”代表integer(整数),表示该函数接受整数作为参数来定位数据‌
 
iloc使用的代码示例:
 
from lxml import etree
import pandas as pd

tree = etree.parse('test.arxml')

namaspace = {
    'ns': 'http://autosar.org/schema/r4.0',
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance'
}

SR_PortData = {
    'Category': [],
    'Prototype': [],
    'Elements': [],
    'Size': [],
    'Init': []
}

CS_PortData = {
    'Category': [],
    'Prototype': [],
    'Elements': []
}

root = tree.getroot()

ar_pack = root.find('.//ns:APPLICATION-SW-COMPONENT-TYPE', namespaces=namaspace)

ComponentName = ar_pack[0].text

RootOfPorts = root.find('.//ns:PORTS', namespaces=namaspace)

Ports = RootOfPorts.getchildren()

for port in Ports:
    parentName = port[0].getparent().tag

    dataElement = port.find('.//ns:DATA-ELEMENT-REF', namespaces=namaspace)

    SR_FindOrNot = 0
    # check the data element
    if dataElement is not None:
        if 'P-' in parentName:
            SR_PortData['Category'].append('Sender')
            SR_FindOrNot = 1
        if 'R-' in parentName:
            SR_PortData['Category'].append('Receiver')
            SR_FindOrNot = 1

        SR_PortData['Prototype'].append(port[0].text)

        SR_PortData['Elements'].append(dataElement.text.split('/')[-1])

    # find the elements of interface
    initvalues = port.find('.//ns:ELEMENTS', namespaces=namaspace)

    if initvalues is not None:
        SR_PortData['Size'].append(len(initvalues))
        initValueArray = []
        for initvalue in initvalues.iterchildren():
            initValueArray.append(initvalue[0].text)
        SR_PortData['Init'].append(initValueArray)
    else:
        # find the numerical value
        initvalues = port.find('.//ns:INIT-VALUE', namespaces=namaspace)
        if initvalues is not None:
            SR_PortData['Size'].append(len(initvalues))
            initValueArray = []
            for initvalue in initvalues.iterchildren():
                initValueArray.append(initvalue[0].text)
            SR_PortData['Init'].append(initValueArray)
        else:
            if SR_FindOrNot == 1:  # SR Port found but no init value,use 0 instead
                SR_PortData['Size'].append(0)
                SR_PortData['Init'].append([0])

    # find the Servers of P port prototype
    servers = port.findall('.//ns:SERVER-COM-SPEC', namespaces=namaspace)
    if servers is not None and len(servers) != 0:

        CS_PortData['Category'].append('Server')
        CS_PortData['Prototype'].append(port[0].text)

        InterfaceArray = []
        for server in servers:
            # print(server)
            operationRef = server.find('.//ns:OPERATION-REF', namespaces=namaspace)
            if operationRef is not None:
                InterfaceArray.append(operationRef.text.split('/')[-1])
        CS_PortData['Elements'].append(InterfaceArray)

    # find the client of P port prototype
    clients = port.findall('.//ns:CLIENT-COM-SPEC', namespaces=namaspace)
    if clients is not None and len(clients) != 0:

        CS_PortData['Category'].append('Client')
        CS_PortData['Prototype'].append(port[0].text)
        InterfaceArray = []
        for client in clients:
            operationRef = client.find('.//ns:OPERATION-REF', namespaces=namaspace)
            if operationRef is not None:
                print(f'The client is : {operationRef.text}')
                InterfaceArray.append(operationRef.text.split('/')[-1])
        CS_PortData['Elements'].append(InterfaceArray)

# Write it to excel
df1 = pd.DataFrame(SR_PortData)
df2 = pd.DataFrame(CS_PortData)
fileName = ComponentName + '.xlsx'

with pd.ExcelWriter(fileName) as writer:
    df1.to_excel(writer, sheet_name='Port1', index=False)  # index=False 表示不写入行索引
    df2.to_excel(writer, sheet_name='Port2', index=False)  # index=False 表示不写入行索引

# 读取刚才保存的文件
df = pd.read_excel(fileName, sheet_name='Port1')
dfkeep = pd.read_excel(fileName, sheet_name='Port2')

# 创建一个新的DataFrame来存储拆分后的数据
new_df = pd.DataFrame(columns=df.columns)

count = 0
for row in df.iterrows(): #遍历每一行
    server = df.loc[count, 'Category']
    port_prototype = df.loc[count, 'Prototype']
    data_elements = df.loc[count, 'Elements']
    data_elements = data_elements.split(',')
    for ele in data_elements:
        ele = ele.replace('[','').replace(']','')
        new_row = pd.DataFrame([[server, prototype, ele.strip()]], columns=df.columns)
        new_df = pd.concat([new_df,new_row], ignore_index=True)
    count += 1

with pd.ExcelWriter(fileName, engine='openpyxl') as writer:
    dfkeep.to_excel(writer, sheet_name='Port1', index=False)
    new_df.to_excel(writer, sheet_name='Port2', index=False)

 


 



posted @ 2025-02-23 13:07  EdwinLee007  阅读(41)  评论(0)    收藏  举报