简体   繁体   English

在台面中从CSV导入代理及其属性

[英]Importing agents and their attributes from CSV in mesa

My data is in .csv format and each row of data represents each agent while each column represents a certain attribute. 我的数据为.csv格式,数据的每一行代表每个代理,而每一列则代表某个属性。

My question is how to assign agents and their attributes from a csv file in Mesa? 我的问题是如何从Mesa中的csv文件分配代理及其属性?

Could anyone help me with how to import them in Mesa please? 有人可以帮我将它们导入Mesa吗?

Thanks. 谢谢。

To import a .csv and turn them into attributes you want to know how you are reading in the file and then pass that into the agent class as you are creating it. 要导入.csv并将其转换为属性,您想知道如何阅读文件,然后在创建文件时将其传递给代理类。

For example: 例如:

you have 'people.csv': 您有“ people.csv”:

         agent, height, weight
         bill, 72 in, 190lbs
         anne, 70 in, 170lbs

you read in as a list of lists: 您以列表列表形式阅读:

import csv
people = []

with open('people.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
         if line_count == 0:
            pass #skips the column headers
            line_count += 1
         else: 
            people.append(row)
            # This will look like [[ 'bill', '72 in', '190lbs'], ['anne', '70 in',\ 
            #'170lbs']]

you pass in a row into your agent instantiation, this would typically occur in the creation of your agent schedule in the model module: 您将一行传递到您的代理实例中,通常会在模型模块中创建代理时间表时发生:

  for i in range(number_of_agents):
        a = myAgent(i, self, row[i])
        self.schedule.add(a)

you assign the row variables to agent attributes: 您将行变量分配给代理属性:

  class myAgent(Agent): 
       def __init__(self, unique_id, model, row): 
            super().__init__(unique_id, model)
            self.name = row[0] # e.g bill
            self.height = row[1] # e.g. 72 in
            self.weight = row[2] # e.g. 190 lbs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Creating a function that reads a csv file to return a 2D list in Python 3.7 without importing csv module - Creating a function that reads a csv file to return a 2D list in Python 3.7 without importing csv module 从Python中的统一类获取属性 - Getting attributes from a unitialized class in Python 除非使用DROP TRIGGER更新物化视图,否则使用Pandas导入.csv to_sql的Python脚本将失败 - Python script for importing .csv to_sql with Pandas fails unless I DROP TRIGGER for updating materialized view 从错误的模块(具有相同名称),VSC导入Python - Python importing from incorrect module (which bears the same name), VSC 如何从PyQt5代码继承实例的属性 - How to inherit attributes of instance from PyQt5 code 从 pmdarima 导入 auto_arima 时:错误:无法从“scipy.misc”导入名称“factorial” - While importing auto_arima from pmdarima: ERROR : cannot import name 'factorial' from 'scipy.misc' 从具有重复标签的 XML 创建 csv - Creating csv from an XML that has repeating tags 如何解决read_csv中的Unicode错误? - How can I resolve a Unicode error from read_csv? 导入 sklearn 时出现导入错误 - ImportError while importing sklearn comtypes.gen 属性与自行下载和 conda 预安装不同 - comtypes.gen attributes are different from self download and conda pre-installation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM