Build a Pandas DataFrame with one unique column of different size

Viewed 19

I want to build the following DataFrame:

num | col1 | col2
a   | 1    | 3
    | 2    | 4
    | 8    | 2
b   | 0    | 9
    | 4    | 2
    | 3    | 1

I tried the following but did not know how of include the column num

col1 = [1, 2, 8, 0, 4, 3]
col2 = [3, 4, 2, 9, 2, 1]
num =  ['a', 'b']
d = pd.DataFrame({'col1': col1, 'col2': col2}, index=num)
1 Answers
col1 = [1, 2, 8, 0, 4, 3]
col2 = [3, 4, 2, 9, 2, 1]
num =  ['a']*3 + ['b']*3    # same as ['a', 'a', 'a', 'b', 'b', 'b']
d = pd.DataFrame({'col1': col1, 'col2': col2}, index=num)
>>> d
   col1  col2
a     1     3
a     2     4
a     8     2
b     0     9
b     4     2
b     3     1
Related