Dark Matter
Dark Matter

Reputation: 200

Pandas is not appending dataframe

I am trying this but df is returning blank textbox values are list like (54.0, 60.18, 86.758, 73.71) Think like having csv file whose header are x y h w and textbox values will get appended in it

import pandas
list111 = [(72.0, 578.18, 378.0, 591.71),(54.0, 564.18, 378.0, 577.71),(54.0, 550.18, 378.0, 563.71),(54.0, 536.18, 378.0, 549.71)]
df = pd.DataFrame()
print df
list_title = ["x","y","h","w"]
for textbox in list111:
    zipped=zip(list_title,textbox)
    df1 = pd.DataFrame(zipped)
    df.append(df1,ignore_index=True)
    print df1,df

Upvotes: 1

Views: 1404

Answers (2)

jezrael
jezrael

Reputation: 863791

You need append DataFrames to list dfs and then use concat with parameter axis=1:

import pandas as pd
list111 = [ (72.0, 578.18, 378.0, 591.71),
            (54.0, 564.18, 378.0, 577.71),
            (54.0, 550.18, 378.0, 563.71),
            (54.0, 536.18, 378.0, 549.71)]

dfs = []
list_title = ["x","y","h","w"]
for textbox in list111:
    zipped=zip(list_title,textbox)
    df1 = pd.DataFrame(zipped)
    dfs.append(df1)

df = pd.concat(dfs, axis=1, ignore_index=True)
print df
   0       1  2       3  4       5  6       7
0  x   72.00  x   54.00  x   54.00  x   54.00
1  y  578.18  y  564.18  y  550.18  y  536.18
2  h  378.00  h  378.00  h  378.00  h  378.00
3  w  591.71  w  577.71  w  563.71  w  549.71

If you need one common column as index:

import pandas as pd

list111 = [ (72.0, 578.18, 378.0, 591.71),
            (54.0, 564.18, 378.0, 577.71),
            (54.0, 550.18, 378.0, 563.71),
            (54.0, 536.18, 378.0, 549.71)]

dfs = []
list_title = ["x","y","h","w"]
for textbox in list111:
    zipped=zip(list_title,textbox)
    df1 = pd.DataFrame(zipped)
    #set first column to index
    df1.set_index(df1.iloc[:,0], inplace =True)
    #append only second column (first is index)
    dfs.append(df1.iloc[:,1])

df = pd.concat(dfs, axis=1, ignore_index=True)
df.index.name = None
print df
        0       1       2       3
x   72.00   54.00   54.00   54.00
y  578.18  564.18  550.18  536.18
h  378.00  378.00  378.00  378.00
w  591.71  577.71  563.71  549.71

But I think the better is use DataFrame constructor with T:

import pandas as pd

list111 = [ (72.0, 578.18, 378.0, 591.71),
            (54.0, 564.18, 378.0, 577.71),
            (54.0, 550.18, 378.0, 563.71),
            (54.0, 536.18, 378.0, 549.71)]


list_title = ["x","y","h","w"]
print pd.DataFrame([li for li in list111], columns=list_title).T
        0       1       2       3
x   72.00   54.00   54.00   54.00
y  578.18  564.18  550.18  536.18
h  378.00  378.00  378.00  378.00
w  591.71  577.71  563.71  549.71

Upvotes: 2

rde
rde

Reputation: 31

You almost got it. Just change the way you construct the DataFrame

import pandas as pd
list111 = [(72.0, 578.18, 378.0, 591.71),(54.0, 564.18, 378.0, 577.71),(54.0, 550.18, 378.0, 563.71),(54.0, 536.18, 378.0, 549.71)]
list_title = ["x","y","h","w"]

df = pd.DataFrame( data =list111, columns = list_title )
print df

prints:

    x       y    h       w
0  72  578.18  378  591.71
1  54  564.18  378  577.71
2  54  550.18  378  563.71
3  54  536.18  378  549.71

Upvotes: 1

Related Questions