坚持使用 pandas 构建 RPG 项目生成器
Stuck using pandas to build RPG item generator
我正在尝试为我正在开发的游戏构建一个简单的随机物品生成器。
到目前为止,我一直在试图弄清楚如何存储和访问所有数据。我 pandas 使用 .csv 文件存储数据集。
我想为生成的项目添加加权概率,因此我尝试读取 csv 文件并将每个列表编译成一个新集合。
我让程序选择一个随机集合,但在尝试从该集合中随机抽取一行时卡住了。
当我使用 .sample() 提取项目行时出现错误,这让我觉得我不明白 pandas 是如何工作的。我想我需要创建新的列表,这样我以后可以在选择一个项目后索引和访问项目的各种统计信息。
一旦我拉出我打算添加会改变伤害和护甲等效果的物品。所以我在考虑让新项目成为它自己的列表,然后使用 damage = item[2] + 3 或者我需要的任何东西
错误是:AttributeError:'list'对象没有属性'sample'
谁能帮忙解决这个问题?也许有更好的方法来设置数据?
到目前为止,这是我的代码:
import pandas as pd
import random
df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]
def get_item():
item_class = [random.choices(df, weights=(45,40,15), k=1)] #this part seemed to work. When I printed item_class it printed one of the entire lists at the correct odds
item = item_class.sample()
print (item) #to see if the program is working
get_item()
我认为您对列表与列表元素有点混淆。这应该工作。我用简单的
存根了你的 dfs
import pandas as pd
import random
# Actual data. Comment it out if you do not have the csv files
df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]
# My stubs -- uncomment and use this instead of the line above if you want to run this specific example
# df = [pd.DataFrame({'weapons' : ['w1','w2']}), pd.DataFrame({'armor' : ['a1','a2', 'a3']}), pd.DataFrame({'aether' : ['e1','e2', 'e3', 'e4']})]
def get_item():
# I removed [] from the line below -- choices() already returns a list of length 1
item_class = random.choices(df, weights=(45,40,15), k=1)
# I added [0] to choose the first element of item_class which is a list of length 1 from the line above
item = item_class[0].sample()
print (item) #to see if the program is working
get_item()
打印我设置的随机数据帧中的随机行,例如
weapons
1 w2
我正在尝试为我正在开发的游戏构建一个简单的随机物品生成器。 到目前为止,我一直在试图弄清楚如何存储和访问所有数据。我 pandas 使用 .csv 文件存储数据集。
我想为生成的项目添加加权概率,因此我尝试读取 csv 文件并将每个列表编译成一个新集合。
我让程序选择一个随机集合,但在尝试从该集合中随机抽取一行时卡住了。
当我使用 .sample() 提取项目行时出现错误,这让我觉得我不明白 pandas 是如何工作的。我想我需要创建新的列表,这样我以后可以在选择一个项目后索引和访问项目的各种统计信息。
一旦我拉出我打算添加会改变伤害和护甲等效果的物品。所以我在考虑让新项目成为它自己的列表,然后使用 damage = item[2] + 3 或者我需要的任何东西
错误是:AttributeError:'list'对象没有属性'sample'
谁能帮忙解决这个问题?也许有更好的方法来设置数据?
到目前为止,这是我的代码:
import pandas as pd
import random
df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]
def get_item():
item_class = [random.choices(df, weights=(45,40,15), k=1)] #this part seemed to work. When I printed item_class it printed one of the entire lists at the correct odds
item = item_class.sample()
print (item) #to see if the program is working
get_item()
我认为您对列表与列表元素有点混淆。这应该工作。我用简单的
存根了你的 dfsimport pandas as pd
import random
# Actual data. Comment it out if you do not have the csv files
df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]
# My stubs -- uncomment and use this instead of the line above if you want to run this specific example
# df = [pd.DataFrame({'weapons' : ['w1','w2']}), pd.DataFrame({'armor' : ['a1','a2', 'a3']}), pd.DataFrame({'aether' : ['e1','e2', 'e3', 'e4']})]
def get_item():
# I removed [] from the line below -- choices() already returns a list of length 1
item_class = random.choices(df, weights=(45,40,15), k=1)
# I added [0] to choose the first element of item_class which is a list of length 1 from the line above
item = item_class[0].sample()
print (item) #to see if the program is working
get_item()
打印我设置的随机数据帧中的随机行,例如
weapons
1 w2