Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create rows and append to dataframe

I would like to create a dataframe with the rows and columns defined like this

     1   2   3   4
 A
 B
 C

and fill it with two loops. I have written this:

list1 = ['1', '2', '3', '4']
list2 = ['A', 'B', 'C']

df1 = pd.DataFrame()
cnt = 0
for l2 in range(len(list2)):
    vec = []
    for l1 in range(len(list1)):
        vec.append(cnt)
        cnt += 1
    tmp_row = {list2[l2], vec}   # <-- Error
    tmp = pd.DataFrame([tmp_row])
    df1 = pd.concat([df1, tmp], axis=0, ignore_index=True)
    print(df1)

So at the first iteration of outer loop, I expect to have

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     1   2   3   4
 A   0   1   2   3

and then

     1   2   3   4
 A   0   1   2   3
 B   4   5   6   7

and so on. However, at the tmp_row, I get this error

TypeError: unhashable type: 'list'

How can I fix the error?


Update:

In this snippet, I used a counter. In my code, that is not a simple counter. So, assume something like:

cnt = foo()
vec.append(cnt)

So, a row is something like A 0.2 0.41 -0.03 0.1 and so on.

>Solution :

If you really want to use you loop (I still don’t get why), here is how to fix it:

list1 = ['1', '2', '3', '4']
list2 = ['A', 'B', 'C']

df1 = pd.DataFrame()
cnt = 0
for l2 in range(len(list2)):
    vec = []
    for l1 in range(len(list1)):
        vec.append(cnt)
        cnt += 1
    tmp = pd.DataFrame([vec], index=[list2[l2]])
    df1 = pd.concat([df1, tmp], axis=0)
    print(df1, end='\n\n')

output:

   0  1  2  3
A  0  1  2  3

   0  1  2  3
A  0  1  2  3
B  4  5  6  7

   0  1   2   3
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11

alternative

probably much more efficient, collect all the data and create the DataFrame in the end:

list1 = ['1', '2', '3', '4']
list2 = ['A', 'B', 'C']


cnt = 0
d = {}
for l2 in range(len(list2)):
    vec = []
    for l1 in range(len(list1)):
        vec.append(cnt)
        cnt += 1
    d[list2[l2]] = vec
    
df1 = pd.DataFrame.from_dict(d, orient='index')

output:

   0  1   2   3
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading