被困在使用熊猫建立RPG项目生成器



我正在为我正在开发的游戏构建一个简单的随机项生成器。到目前为止,我一直在想如何存储和访问所有数据。我和熊猫一起使用.csv文件来存储数据集。

我想给生成的项目添加加权概率,所以我尝试读取csv文件,并将每个列表编译成一个新的集合。

我让程序选择了一个随机集合,但在试图从该集合中提取随机行时遇到了问题。

当我使用.sample((来提取项目行时,我遇到了一个错误,这让我觉得我不明白panda是如何工作的。我想我需要创建新的列表,这样我以后可以在选择一个列表后索引和访问项目的各种统计信息。

一旦我拔出物品,我打算添加一些效果来改变伤害和护甲等等。所以我想让新物品成为自己的清单,然后使用损坏=物品[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

最新更新