Gustavo Amarante
Gustavo Amarante

Reputation: 242

Converting List to pandas DataFrame

I need to build a DataFrame with a very specific structure. Yield curve values as the data, a single date as the index, and days to maturity as the column names.

In[1]: yield_data  # list of size 38, with yield values
Out[1]: 
[0.096651956137087325,
 0.0927199778042056,
 0.090000225505577847,
 0.088300016028163508,...


In[2]: maturity_data  # list of size 38, with days until maturity
Out[2]:
[6,
 29,
 49,
 70,...

In[3]: today
Out[3]: 
Timestamp('2017-07-24 00:00:00')

Then I try to create the DataFrame

pd.DataFrame(data=yield_data, index=[today], columns=maturity_data)

but it returns the error

ValueError: Shape of passed values is (1, 38), indices imply (38, 1)

I tried using the transpose of these lists, but it does not allow to transpose them.

how can I create this DataFrame?

Upvotes: 2

Views: 5700

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

IIUC, I think you want a dataframe with a single row, you need to reshape your data input list into a list of list.

yield_data = [0.09,0.092, 0.091]
maturity_data = [6,10,15]
today = pd.to_datetime('2017-07-25')

pd.DataFrame(data=[yield_data],index=[today],columns=maturity_data)

Output:

              6      10     15
2017-07-25  0.09  0.092  0.091

Upvotes: 1

Related Questions