1

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
2
  • I want all dataframes append in df Commented Mar 2, 2016 at 12:49
  • 1
    Then state it in the code df = df.append(df1,ignore_index=True). The interpreter is unable to guess your intent. You are telling it to append the dataframes and then ignore the result. Commented Mar 2, 2016 at 12:59

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanx for help ...can we have all x, y,h,w in same coloumn ??
Hmmm, I dont understand - all x, y,h,w are in same column. Can you explain it?
I want data like x y h w 1 2 3 4 3 4 6 8
Thanx buddy...saved lot of time :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.