Home > front end >  Create rows and append to dataframe
Create rows and append to dataframe

Time:05-25

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

     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.

CodePudding user response:

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

CodePudding user response:

You can do this with a list comprehension inside the dataframe generation (assuming that foo() returns a list of len(list1) values):

df = pd.DataFrame([foo() for _ in range(len(list2))], columns=list1, index=list2)

Should foo() be a counter, the result would be:

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

CodePudding user response:

You may can create the dataframe without loop

out = pd.DataFrame(np.arange(12).reshape(-1,4), columns = list1, index = list2)
Out[14]: 
   1  2   3   4
A  0  1   2   3
B  4  5   6   7
C  8  9  10  11
  • Related