1

This is similar to this post "Appending to an empty data frame in Pandas?" but what I want is I have 2 lists

titles = ['Style','Occupancy','Ext_Wall_2','Roof_Cover','Int_Wall_2','Int_flr_2','Heat_Type','TTL_Bed','TTl_Half_Bath','TTL_Rooms','Kit_Style']

Bld_list = ['Colonial', '1', '\xa0', 'Asphalt', '\xa0', '\xa0','Forced Air-Duc',
 '4 Bedrooms', '2', '8',  'Average']

I want to create a dataframe that would have titles as column headers across and ten row 0 of the dataframe be the bld_list value so it would be 1 x 11 dataframe

I tried the follow from the post but it gave the following error >>> "unhashable type: 'list'". I think the answer has to be very simple but I cant figure it out. Let me know if you can help. thank you

df6 = pd.DataFrame()
data = pd.DataFrame({columns:bld_list})
df6.append(data)

3 Answers 3

2

Is that what you want?

In [88]: pd.DataFrame([Bld_list], columns=titles)
Out[88]:
      Style Occupancy Ext_Wall_2 Roof_Cover Int_Wall_2 Int_flr_2       Heat_Type     TTL_Bed TTl_Half_Bath TTL_Rooms Kit_Style
0  Colonial         1               Asphalt                       Forced Air-Duc  4 Bedrooms             2         8   Average
Sign up to request clarification or add additional context in comments.

Comments

1

You want do something as follows:

import pandas as pd

titles = ['Style','Occupancy','Ext_Wall_2','Roof_Cover','Int_Wall_2','Int_flr_2','Heat_Type','TTL_Bed','TTl_Half_Bath','TTL_Rooms','Kit_Style']

Bld_list = ['Colonial', '1', '\xa0', 'Asphalt', '\xa0', '\xa0', 'Forced Air-Duc', '4 Bedrooms', '2', '8',  'Average']

df = pd.DataFrame({'Title1':titles, 'Title2':Bld_list})
print(df)

3 Comments

That give the titles in DF as Title1 and Title2. and it presents the data vertically in a 11x2 dataframe (2 columns 11 rows). I want the titles to be the titles in the names in the "titles" list and the 1st row (row 0) in the DF to be the info in the "bld_list" list. So it will be 11 columns and 1 row
@JWestwood I misunderstood what you were asking. Please accept Max's answer as it is correct.
Max your suggestion worked. Thank you, Parfait when I pass your comments I get the following >>> If using all scalar values, you must pass an index. Any idea how to edit >> trying to learn new code. thanks
0

Consider a dictionary comprehension:

df <- pd.DataFrame({t:b for t,b in zip(titles, Bld_list)})

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.