用掷骰子制作多索引 df

Making a multiindex df with dice throws

提问人:AndysPythonStuff 提问时间:7/22/2023 更新时间:7/22/2023 访问量:33

问:

我搞砸了熊猫和 numpy,我的课程中有一个教程,用骰子求和两个数字。本教程使用了熊猫,但我也尝试使用 numpy,然后比较了结果。

throws = 50
diepd = pd.DataFrame([1, 2, 3, 4, 5, 6])
dienp = np.array([1,2,3,4,5,6])
np.random.seed(1) 
sum_np = [np.random.choice(dienp,2,True).sum() for i in range(throws)] 
sum_pd = [diepd.sample(2, replace=True).sum().loc[0] for i in range(throws)]

compare = pd.DataFrame(data={'sum_np': sum_np, 'sum_pd': sum_pd})

compare

我在理解/操作多索引数据帧方面遇到了真正的困难,所以作为一个额外的课程,我想学习如何用结果创建一个,比较它们的不同之处(因为我使用相同的种子)。

指数将只是 50 次(1 次投掷)投掷。索引标签(列)将为: 级别 0: 2 列:numpy 结果和 pandas 结果。

1级: 每列三列: 2 个人投掷和总和。 例如,和 的两个值以及各自的总和。np.random.choice(dienp,2,True)diepd.sample(2, replace=True

numpy 熊猫
不。 抛1 抛2 抛1 抛2
1 1 2 3 4 5 9
2 2 3 5 6 1 7
3 4 6 10 5 2 7

有什么建议吗?

python pandas numpy 多索引 随机种子

评论


答:

1赞 Simon Champney 7/22/2023 #1

从代码的角度来看,如果不遍历其自身行上的行并将值附加到嵌入到循环中的列表中,似乎很难获取每个骰子的值。我的解决方案是设置两个不同的表,然后将它们连接在一起。你可以在下面看到我的代码:

import pandas as pd
import numpy as np

throws = 50
diepd = pd.DataFrame([1, 2, 3, 4, 5, 6])
dienp = np.array([1,2,3,4,5,6])
np.random.seed(1)
np_roll=[]
pd_roll=[]
for i in range(3):
    np_roll.append([])
    pd_roll.append([])
for i in range(throws):
    for j in range(2):
        np_roll[j].append(np.random.choice(dienp,1,True).sum())
        pd_roll[j].append(diepd.sample(1, replace=True).sum().loc[0])
        np_roll[j]=list(np_roll[j])
        pd_roll[j]=list(pd_roll[j])
    np_roll[2].append(np_roll[0][i]+np_roll[1][i])
    pd_roll[2].append(pd_roll[0][i]+pd_roll[1][i])
    

np_df = pd.DataFrame(data={'Roll 1': np_roll[0], 'Roll 2': np_roll[1], "Sum": np_roll[2]})
pd_df = pd.DataFrame(data={'Roll 1': pd_roll[0], 'Roll 2': pd_roll[1], "Sum": pd_roll[2]})

compare = pd.concat([np_df, pd_df],axis=1,keys=["Numpy", "Pandas"])

pd.set_option('display.max_columns', None)
print(compare)

评论

0赞 AndysPythonStuff 7/25/2023
谢谢!循环不是问题,我对制作 multindex 更感兴趣。
1赞 MSS 7/22/2023 #2

这可以按如下方式完成

import numpy as np
import pandas as pd

arrays = [
    ["numpy", "numpy", "numpy", "pandas", "pandas", "pandas"],
    ["throw1", "throw2", "sum", "throw1", "throw2", "sum"]
]
tuples = list(zip(*arrays))
col_index = pd.MultiIndex.from_tuples(tuples)

throws = 50
diepd = pd.DataFrame([1, 2, 3, 4, 5, 6])
dienp = np.array([1, 2, 3, 4, 5, 6])
np.random.seed(1)

# Create the throw1 and throw2 columns from dienp
throw1_np = np.random.choice(dienp, throws, replace=True)
throw2_np = np.random.choice(dienp, throws, replace=True)

# Create the throw1 and throw2 columns from diepd
throw1_pd = diepd.sample(throws, replace=True).values
throw2_pd = diepd.sample(throws, replace=True).values

# Create
# Add throw1 and throw2 to obtain sum
sum_np = throw1_np + throw2_np
sum_pd = throw1_pd + throw2_pd

df = pd.DataFrame(np.column_stack([throw1_np, throw2_np, sum_np, throw1_pd, throw2_pd, sum_pd]), columns=col_index)

print(df.head())

评论

0赞 AndysPythonStuff 7/25/2023
这远远超出了我的技能水平。我要花点时间研究它。有些命令我还没有使用过。非常感谢您的帮助!
0赞 AndysPythonStuff 7/25/2023
只是一个想法......假设所需的多索引 DF 有数百个 0 级和 1 级标签......你还会使用这种方法、元组和MI.from_tuples吗?例如,级别 0 = “州”,级别 1 = “城市”,或者级别 0 = “公司”,级别 1 = 各种财务价值?