• <legend id='FWXvs'><style id='FWXvs'><dir id='FWXvs'><q id='FWXvs'></q></dir></style></legend>
      <bdo id='FWXvs'></bdo><ul id='FWXvs'></ul>

      1. <tfoot id='FWXvs'></tfoot>

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

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

        使用循环填充空的python数据框

        时间:2023-10-19
        <i id='FXFLH'><tr id='FXFLH'><dt id='FXFLH'><q id='FXFLH'><span id='FXFLH'><b id='FXFLH'><form id='FXFLH'><ins id='FXFLH'></ins><ul id='FXFLH'></ul><sub id='FXFLH'></sub></form><legend id='FXFLH'></legend><bdo id='FXFLH'><pre id='FXFLH'><center id='FXFLH'></center></pre></bdo></b><th id='FXFLH'></th></span></q></dt></tr></i><div id='FXFLH'><tfoot id='FXFLH'></tfoot><dl id='FXFLH'><fieldset id='FXFLH'></fieldset></dl></div>
      3. <tfoot id='FXFLH'></tfoot>
          <bdo id='FXFLH'></bdo><ul id='FXFLH'></ul>
            <tbody id='FXFLH'></tbody>

          1. <legend id='FXFLH'><style id='FXFLH'><dir id='FXFLH'><q id='FXFLH'></q></dir></style></legend>

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

                  本文介绍了使用循环填充空的python数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我想用循环中的值创建并填充一个空数据框.

                  Lets say I want to create and fill an empty dataframe with values from a loop.

                  import pandas as pd
                  import numpy as np
                  
                  years = [2013, 2014, 2015]
                  dn=pd.DataFrame()
                  for year in years:
                      df1 = pd.DataFrame({'Incidents': [ 'C', 'B','A'],
                                   year: [1, 1, 1 ],
                                  }).set_index('Incidents')
                      print (df1)
                      dn=dn.append(df1, ignore_index = False)
                  

                  即使忽略索引为假,追加也会给出对角矩阵:

                  The append gives a diagonal matrix even when ignore index is false:

                  >>> dn
                         2013  2014  2015
                  Incidents                  
                  C             1   NaN   NaN
                  B             1   NaN   NaN
                  A             1   NaN   NaN
                  C           NaN     1   NaN
                  B           NaN     1   NaN
                  A           NaN     1   NaN
                  C           NaN   NaN     1
                  B           NaN   NaN     1
                  A           NaN   NaN     1
                  
                  [9 rows x 3 columns]
                  

                  应该是这样的:

                  >>> dn
                         2013  2014  2015
                  Incidents                  
                  C             1   1   1
                  B             1   1   1
                  A             1   1   1
                  
                  [3 rows x 3 columns]
                  

                  有没有更好的方法来做到这一点?有没有办法修复附加?

                  Is there a better way of doing this? and is there a way to fix the append?

                  我有熊猫版本'0.13.1-557-g300610e'

                  I have pandas version '0.13.1-557-g300610e'

                  推荐答案

                  import pandas as pd
                  
                  years = [2013, 2014, 2015]
                  dn = []
                  for year in years:
                      df1 = pd.DataFrame({'Incidents': [ 'C', 'B','A'],
                                   year: [1, 1, 1 ],
                                  }).set_index('Incidents')
                      dn.append(df1)
                  dn = pd.concat(dn, axis=1)
                  print(dn)
                  

                  产量

                             2013  2014  2015
                  Incidents                  
                  C             1     1     1
                  B             1     1     1
                  A             1     1     1
                  

                  <小时>

                  请注意,在循环外调用 pd.concat once 更省时而不是在循环的每次迭代中调用 pd.concat.


                  Note that calling pd.concat once outside the loop is more time-efficient than calling pd.concat with each iteration of the loop.

                  每次调用 pd.concat 都会为新的 DataFrame 分配新空间,并且每个组件 DataFrame 中的所有数据都被复制到新的 DataFrame 中.如果你从 for 循环中调用 pd.concat 然后你最终在订单上做n**2 个副本,其中 n 是年数.

                  Each time you call pd.concat new space is allocated for a new DataFrame, and all the data from each component DataFrame is copied into the new DataFrame. If you call pd.concat from within the for-loop then you end up doing on the order of n**2 copies, where n is the number of years.

                  如果您将部分 DataFrames 累积在一个列表中并调用一次 pd.concat在列表之外,那么 Pandas 只需要执行 n 个副本即可制作 dn.

                  If you accumulate the partial DataFrames in a list and call pd.concat once outside the list, then Pandas only needs to perform n copies to make dn.

                  这篇关于使用循环填充空的python数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:只能通过 csv 阅读器迭代一次 下一篇:为什么 Python 'for word in words:' 迭代单个字符而不是单词?

                  相关文章

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

                  <legend id='HVoj2'><style id='HVoj2'><dir id='HVoj2'><q id='HVoj2'></q></dir></style></legend>

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

                    • <bdo id='HVoj2'></bdo><ul id='HVoj2'></ul>
                    1. <tfoot id='HVoj2'></tfoot>