AgentList Operations
Operations for manipulating collections of agents.
Naming Agents
Set Names from Traits
python
# Name agents using trait values (removes used traits by default)
agents = agents.give_names("respondent_id")
# Keep the traits used for naming
agents = agents.give_names("first_name", "last_name", remove_traits=False)
# Custom separator for multiple traits
agents = agents.give_names("city", "id", separator="_")
# Names: "NYC_001", "LA_002", etc.
Assign UUID Names
python
agents = agents.give_uuid_names()
Sampling and Shuffling
python
# Random sample (returns new AgentList) sample = agents.sample(n=10, seed=42) # Shuffle order (returns new AgentList) shuffled = agents.shuffle(seed=42) # Split into two groups train, test = agents.split(frac_left=0.8, seed=42)
Combining AgentLists
python
# Concatenate (must have same traits and codebook) combined = agents1 + agents2 # Collapse agents with same name (merges their traits) collapsed = agents.collapse()
Applying Deltas (Batch Updates)
python
from edsl import AgentDelta, AgentListDeltas
# Create deltas for named agents
deltas = AgentListDeltas({
"Alice": AgentDelta({"age": 31, "status": "promoted"}),
"Bob": AgentDelta({"age": 26})
})
# Apply to agent list (returns new AgentList)
updated_agents = agents.apply_deltas(deltas)
Conversion
python
# To Dataset dataset = agents.to_dataset() # To ScenarioList scenarios = agents.to_scenario_list() # To pandas DataFrame df = agents.to_pandas()
Accessing Agents
python
# By index
agent = agents[0]
agent = agents[-1]
# Slicing
subset = agents[0:5]
# By position
first = agents.first()
last = agents.last()
agent = agents.at(3)
# Iteration
for agent in agents:
print(agent.traits)
Running Surveys with Agents
python
from edsl import Survey, QuestionFreeText
survey = Survey([
QuestionFreeText(question_name="opinion", question_text="What do you think?")
])
# Run with agents
results = survey.by(agents).run()
# Or using agent.to()
results = agents.to(survey).run()
Quick Reference
| Operation | Method |
|---|---|
| Name from traits | agents.give_names("trait") |
| UUID names | agents.give_uuid_names() |
| Sample | agents.sample(n=10, seed=42) |
| Shuffle | agents.shuffle(seed=42) |
| Split | agents.split(frac_left=0.8) |
| Combine | agents1 + agents2 |
| Collapse | agents.collapse() |
| Apply deltas | agents.apply_deltas(deltas) |
| To Dataset | agents.to_dataset() |
| To ScenarioList | agents.to_scenario_list() |
| To DataFrame | agents.to_pandas() |
| First/Last | agents.first(), agents.last() |
| Length | len(agents) |