<i id='AYMLt'><tr id='AYMLt'><dt id='AYMLt'><q id='AYMLt'><span id='AYMLt'><b id='AYMLt'><form id='AYMLt'><ins id='AYMLt'></ins><ul id='AYMLt'></ul><sub id='AYMLt'></sub></form><legend id='AYMLt'></legend><bdo id='AYMLt'><pre id='AYMLt'><center id='AYMLt'></center></pre></bdo></b><th id='AYMLt'></th></span></q></dt></tr></i><div id='AYMLt'><tfoot id='AYMLt'></tfoot><dl id='AYMLt'><fieldset id='AYMLt'></fieldset></dl></div>

<small id='AYMLt'></small><noframes id='AYMLt'>

  • <legend id='AYMLt'><style id='AYMLt'><dir id='AYMLt'><q id='AYMLt'></q></dir></style></legend>

        • <bdo id='AYMLt'></bdo><ul id='AYMLt'></ul>

        <tfoot id='AYMLt'></tfoot>

        元组到 DataFrame 转换的列表

        时间:2023-09-01
        <tfoot id='wkiYQ'></tfoot>

            <tbody id='wkiYQ'></tbody>

          <i id='wkiYQ'><tr id='wkiYQ'><dt id='wkiYQ'><q id='wkiYQ'><span id='wkiYQ'><b id='wkiYQ'><form id='wkiYQ'><ins id='wkiYQ'></ins><ul id='wkiYQ'></ul><sub id='wkiYQ'></sub></form><legend id='wkiYQ'></legend><bdo id='wkiYQ'><pre id='wkiYQ'><center id='wkiYQ'></center></pre></bdo></b><th id='wkiYQ'></th></span></q></dt></tr></i><div id='wkiYQ'><tfoot id='wkiYQ'></tfoot><dl id='wkiYQ'><fieldset id='wkiYQ'></fieldset></dl></div>
          <legend id='wkiYQ'><style id='wkiYQ'><dir id='wkiYQ'><q id='wkiYQ'></q></dir></style></legend>

          <small id='wkiYQ'></small><noframes id='wkiYQ'>

              • <bdo id='wkiYQ'></bdo><ul id='wkiYQ'></ul>

                  本文介绍了元组到 DataFrame 转换的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类似于下面的元组列表:

                  I have a list of tuples similar to the below:

                  [(date1, ticker1, value1),(date1, ticker1, value2),(date1, ticker1, value3)]
                  

                  我想将其转换为具有 index=date1columns=ticker1values = values 的 DataFrame.最好的方法是什么?

                  I want to convert this to a DataFrame with index=date1, columns=ticker1, and values = values. What is the best way to do this?

                  我的最终目标是创建一个 datetimeindex 等于 date1 的 DataFrame,其值位于标有ticker"的列中:

                  My end goal is to create a DataFrame with a datetimeindex equal to date1 with values in a column labeled 'ticker':

                  df = pd.DataFrame(tuples, index=date1)
                  

                  现在元组生成如下:

                  tuples=list(zip(*prc_path))
                  

                  其中 prc_path 是一个形状为 (1000,1) 的 numpy.ndarray

                  where prc_path is a numpy.ndarray with shape (1000,1)

                  推荐答案

                  我想这就是你想要的:

                  >>> data = [('2013-01-16', 'AAPL', 1),
                              ('2013-01-16', 'GOOG', 1.5),
                              ('2013-01-17', 'GOOG', 2),
                              ('2013-01-17', 'MSFT', 4),
                              ('2013-01-18', 'GOOG', 3),
                              ('2013-01-18', 'MSFT', 3)]
                  
                  >>> df = pd.DataFrame(data, columns=['date', 'ticker', 'value'])
                  >>> df
                           date ticker  value
                  0  2013-01-16   AAPL    1.0
                  1  2013-01-16   GOOG    1.5
                  2  2013-01-17   GOOG    2.0
                  3  2013-01-17   MSFT    4.0
                  4  2013-01-18   GOOG    3.0
                  5  2013-01-18   MSFT    3.0
                  
                  >>> df.pivot('date', 'ticker', 'value')
                  ticker      AAPL  GOOG  MSFT
                  date                        
                  2013-01-16     1   1.5   NaN
                  2013-01-17   NaN   2.0     4
                  2013-01-18   NaN   3.0     3
                  

                  这篇关于元组到 DataFrame 转换的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python 中的 *tuple 和 **dict 是什么意思? 下一篇:将一列字符串转换为 pandas 列表

                  相关文章

                  <i id='k7NrR'><tr id='k7NrR'><dt id='k7NrR'><q id='k7NrR'><span id='k7NrR'><b id='k7NrR'><form id='k7NrR'><ins id='k7NrR'></ins><ul id='k7NrR'></ul><sub id='k7NrR'></sub></form><legend id='k7NrR'></legend><bdo id='k7NrR'><pre id='k7NrR'><center id='k7NrR'></center></pre></bdo></b><th id='k7NrR'></th></span></q></dt></tr></i><div id='k7NrR'><tfoot id='k7NrR'></tfoot><dl id='k7NrR'><fieldset id='k7NrR'></fieldset></dl></div>

                    • <bdo id='k7NrR'></bdo><ul id='k7NrR'></ul>
                    <legend id='k7NrR'><style id='k7NrR'><dir id='k7NrR'><q id='k7NrR'></q></dir></style></legend>
                    1. <small id='k7NrR'></small><noframes id='k7NrR'>

                    2. <tfoot id='k7NrR'></tfoot>