将列表转换为 Pandas DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45306556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting List to pandas DataFrame
提问by Gustavo Amarante
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.
我需要构建一个具有非常特定结构的 DataFrame。收益率曲线值作为数据,单个日期作为索引,到期天数作为列名。
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
然后我尝试创建 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?
我怎样才能创建这个数据帧?
采纳答案by Scott Boston
IIUC, I think you want a dataframe with a single row, you need to reshape your data input list into a list of list.
IIUC,我认为您想要一个单行的数据框,您需要将数据输入列表重塑为列表列表。
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

