1

Bellow are the steps on how I would like to get my data saved:

Step 1

ID A B
1 rand
4 rand

Step 2

ID A B
1 rand rand
4 rand rand

Step 3

ID A B
1 rand rand
4 rand rand
3 rand
6 rand

Step 4

ID A B
1 rand rand
4 rand rand
3 rand rand
6 rand rand

Current code:

from numpy.random import rand
import pandas as pd
IDS = [[1,4],[3,6]]
raws = ['A','B']
df = pd.DataFrame(columns=['ID']+raws)
print(df)

for ind in IDS:
    df.index = ind
    for raw in raws:
        df[ind][raw] = rand(len(ind))

Issue: At the moment I have the following error: "ValueError: Length mismatch: Expected axis has 0 elements, new values have 2 elements"

1 Answer 1

1

I am sure that there are way more pythonic solutions, but I could think of this:

from numpy.random import rand
import pandas as pd
IDS = [[1,4],[3,6]]
raws = ['A','B']
df = pd.DataFrame(columns=raws)
print(df)

for ind in IDS:

    for i in ind:
        df = df.append(pd.Series(name = i, dtype='object'))

    for raw in raws:
        for i in ind:
            df[raw][i] = rand(len(ind))
        print(df)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.