Raghavan vmvs
Raghavan vmvs

Reputation: 1265

Create a dataframe and populate the same using for loops in pandas

I have three lists in Python that are made of character vectors

 A=["A1", "B1", "C1"]
 B=["E1", "F1"]
 C=[]

I have created a list using this as follows

  import pandas as pd
  files_list=[]
  files_list.append(A)
  files_list.append(B) 
  files_list.append(C) 

I am trying to create the following dataframe

   Value    Index
   A1       A
   B1       A
   C1       A
   D1       B
   E1       B
            C

I have used the following loop

 L = pd.DataFrame()
 for file in files_list:
             page = file
             for j in page:
             #print (j)
             L["Value"]=j
             L["Index"]=file

I am not getting the desired output but an error saying Length of values (2) does not match length of index (3).

I also tried the following code

 L = pd.DataFrame()
    for file in files_list:
    page = file
    for j in page:
    data = list(zip((j), file))

This is also not yielding the required output and only lists few of the entries I request someone to take a look

Upvotes: 1

Views: 55

Answers (1)

jezrael
jezrael

Reputation: 862641

I suggest create dictionary here and then flatten values to list of tuples passed to DataFrame constructor:

A=["A1", "B1", "C1"]
B=["E1", "F1"]
C=[]
  
d = {'A':A, 'B':B, 'C':C}

out = []
for x, y in d.items():
    if len(y) > 0:
        for a in y:
            out.append((a, x))
    else:
        out.append((np.nan, x))
print (out)
[('A1', 'A'), ('B1', 'A'), ('C1', 'A'), ('E1', 'B'), ('F1', 'B'), (nan, 'C')]

df = pd.DataFrame(out, columns=['Value','Value'])
print (df)
  Value Value
0    A1     A
1    B1     A
2    C1     A
3    E1     B
4    F1     B
5   NaN     C

Upvotes: 1

Related Questions