0

これはかなり複雑な質問なので、準備してください!EAV テーブルのテスト データを Excel で生成したいと考えています。私が持っている列は次のとおりです。

user_id、属性、値

各 user_id は 1 ~ 4 の間のランダムな回数繰り返され、エントリごとにリストからランダムな属性を選択し、次にこれが取ることができるランダムな値を選択します。最後に、各 ID エントリの属性を一意にする必要があります。つまり、同じ ID と属性を持つ複数のエントリは必要ありません。以下は、私が意味するものの例です。

user_id attribute   value
100001  gender      male
100001  religion    jewish
100001  university  imperial
100002  gender      female
100002  course      physics

可能な値:

attribute   value
gender      male
            female
course      maths
            physics
            chemistry
university  imperial
            cambridge
            oxford
            ucl
religion    jewish
            hindu
            christian
            muslim

上の表がおかしくてすみません。構造を保持したままこ​​こに貼り付ける方法がわかりません。うまくいけば、私が話していることがわかると思います。さもなければ、スクリーンショットを取得できます。

これどうやってするの?過去に、乱数ジェネレーターと VLOOKUP を使用してランダム データを生成しましたが、これは私のリーグから少し外れています。

4

2 に答える 2

1

私のアプローチは、ID ごとに 4 つの属性すべてを含むテーブルを作成し、そのテーブルをランダムにフィルター処理して、ID ごとにフィルター処理された 1 ~ 4 行を取得することです。各属性にランダムな値を割り当てました。基本的なセットアップは次のようになります。

ルックアップ テーブル付きのランダム化された eav テーブル

左側はランダム化された eav テーブルで、左側はランダム化された値に使用されるルックアップ テーブルです。ここに式があります。それらを入力してコピーします:

列 A - 4 桁ごとに乱数を設定します。これにより、選択する必要がある属性が決まります。

=IF(COUNTIF(C$2:C2,C2)=1,RANDBETWEEN(1,4),A1)

列 B - A の数式を使用して、行が含まれているかどうかを判断します。

=IF(COUNTIF(C$2:C2,C2)=A2,TRUE,RANDBETWEEN(0,1)=1)

列 C - 100,001 から始まる ID を作成します。

=(INT((ROW()-2)/4)+100000)+1

列 D - 4 つの属性を繰り返します。

=CHOOSE(MOD(ROW()-2,4)+1,"gender","course","university","religion")

列 E - ルックアップ テーブルで最初に出現する列 D 属性を検索し、ランダムなオフセット値を選択します。

=INDEX($H$2:$H$14,(MATCH(D2,$G$2:$G$14,0))+RANDBETWEEN(0,COUNTIF($G$2:$G$14,D2)-1))

列 B の TRUE でフィルタリングすると、ID ごとに 1 ~ 4 個の属性のリストが表示されます。残念なことに、フィルタリングによって再計算が強制されるため、フィルタリングされたリストは、列 B のすべてのセルに対して TRUE とは言えなくなります。

これが私のものだったら、おそらく「魔法の数」4を独自のセル(属性の数)に入れて、もう少し自動化します。

于 2012-08-18T22:15:01.430 に答える
0

これを行うにはいくつかの方法があります。perl または python のいずれかを使用できます。どちらにも、スプレッドシートを操作するためのモジュールがあります。この場合、python と openpyxl モジュールを使用しました。

# File:  datagen.py
# Usage: datagen.py <excel (.xlsx) filename to store data>
# Example:  datagen.py myfile.xlsx

import sys
import random
from openpyxl import Workbook
from openpyxl.cell import get_column_letter

# verify that user specified an argument
if len(sys.argv) < 2:
    print "Specify an excel filename to save the data, e.g myfile.xlsx"
    exit(-1)

# get the excel workbook and worksheet objects
wb = Workbook()
ws = wb.get_active_sheet()

# Modify this line to specify the range of user ids
ids = range(100001, 100100)

# data structure for the attributes and values
data = { 'gender':      ['male',    'female'], 
         'course':      ['maths',   'physics',  'chemistry'],
         'university':  ['imperial','cambridge','oxford',   'ucla'],
         'religion':    ['jewish',  'hindu',    'christian','muslim']}

# Write column headers in the spreadsheet          
ws.cell('%s%s'%('A', 1)).value = 'user_id'
ws.cell('%s%s'%('B', 1)).value = 'attribute'
ws.cell('%s%s'%('C', 1)).value = 'value'

row = 1

# Loop through each user id
for user_id in ids:
    # randomly select how many attributes to use
    attr_cnt = random.randint(1,4)
    attributes = data.keys()
    for idx in range(attr_cnt):
        # randomly select attribute
        attr = random.choice(attributes)
        # remove the selected attribute from further selection for this user id
        attributes.remove(attr)
        # randomly select a value for the attribute
        value = random.choice(data[attr])
        row = row + 1
        # write the values for the current row in the spreadsheet
        ws.cell('%s%s'%('A', row)).value = user_id
        ws.cell('%s%s'%('B', row)).value = attr
        ws.cell('%s%s'%('C', row)).value = value

# save the spreadsheet using the filename specified on the cmd line
wb.save(filename = sys.argv[1]) 
print "Done!"
于 2012-08-18T22:17:48.283 に答える