Reputation: 1088
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"
Upvotes: 1
Views: 44
Reputation: 397
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)
Upvotes: 1