简体   繁体   English

来自列,DataFrame,pandas的随机值组合

[英]Random values combination from columns, DataFrame, pandas

I have next DataFrame in pandas: 我在熊猫中有下一个DataFrame:

A  B
1  23
43 446
197 5
99 12
....

What I want to have is another DataFrame with the same columns A and B and random elements ( 0 < A_i < A_max , 0 < B_i < B_max ), where every unique combination of A and B elements in some row doesn't exist in the first DataFrame. 我想要的是另一个具有相同列A和B和随机元素( 0 < A_i < A_max0 < B_i < B_max )的0 < B_i < B_max ,其中某行中A和B元素的每个唯一组合都不存在于第一个DataFrame。

If you don't care about the distribution, you can simply use uniform distribution from random . 如果您不关心分布,则可以简单地使用random均匀分布。

Assuming the original DataFrame is named df and you want a random_df of the same length: 假设原始DataFrame名为df并且您想要一个相同长度的random_df

from random import random
import pandas as pd

A_max = df['A'].max()
B_max = df['B'].max()

random_df = pd.DataFrame(columns=df.columns)

i = 0
while i < range(len(df)):
    A_random = int(random() * A_max)
    B_random = int(random() * B_max)

    # Checking that the combination does not exist in the original DataFrame
    if len(df[(df['A'] == A_random) & (df['B'] == B_random)] == 0:
        i += 1
        random_df.append({'A': A_random, 'B': B_random}, ignore_index=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM