如何从数据帧中随机选择n行t次?
例如:我有一个包含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
Output
n1 0.1
n2 0.2
n3 0.3
n3 0.3
n5 0.5
n6 0.6
n1 0.1
n4 0.4
n3 0.3
n2 0.2
n4 0.4
n6 0.6
and so on...
最后,我想用另一个数据框绘制这10个小数据框的得分列。
到目前为止,我已经使用了以下功能。但是我不知道如何获得n次样品。
df_elements = df.sample(n=3)
谢谢
答案 0 :(得分:0)
使用list comprehension
:
df_elements = [x.sample(n=3) for i in range(10)]
如果要加入他们在一起,请使用concat
:
df2 = pd.concat(df_elements, ignore_index=True)
答案 1 :(得分:0)
以下内容将n
个数据帧存储在字典中,以便您可以访问它们:
# Initialize dictionary
samples={}
# Store n samples
for i in range(1,n+1):
samples[i] = df.sample(frac=0.3)
通过这种方式,您将可以通过i-th
访问samples[i]
样本