Pandas 从数据帧t次随机选择n行?
如何从数据帧t次中随机选择n行 例如:我有一个10行的数据帧df,我想一次得到3行,然后重复10次Pandas 从数据帧t次随机选择n行?,pandas,random,Pandas,Random,如何从数据帧t次中随机选择n行 例如:我有一个10行的数据帧df,我想一次得到3行,然后重复10次 a value n1 0.1 n2 0.2 n3 0.3 n4 0.4 n5 0.5 n6 0.6 n7 0.7 n8 0.8 n9 0.9 n10 1 最后,我想用另一个数据帧绘制这10个小数据帧的分数列 到目前为止,我已经使用了下面的函数。但我不知道如何获得n次样品 df_elements = df.sample(n=3) 谢谢使用列表理解: df_elements
a value
n1 0.1
n2 0.2
n3 0.3
n4 0.4
n5 0.5
n6 0.6
n7 0.7
n8 0.8
n9 0.9
n10 1
最后,我想用另一个数据帧绘制这10个小数据帧的分数列
到目前为止,我已经使用了下面的函数。但我不知道如何获得n次样品
df_elements = df.sample(n=3)
谢谢使用
列表理解
:
df_elements = [x.sample(n=3) for i in range(10)]
如果要将它们连接在一起,请使用:
以下内容将在字典中存储
n
数据帧,以便您可以访问它们:
# Initialize dictionary
samples={}
# Store n samples
for i in range(1,n+1):
samples[i] = df.sample(frac=0.3)
这样,您就可以通过
samples[i]
使用for
循环访问i-th
样本
df2 = pd.concat(df_elements, ignore_index=True)
# Initialize dictionary
samples={}
# Store n samples
for i in range(1,n+1):
samples[i] = df.sample(frac=0.3)