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

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

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

        在维护列数据类型的同时将行插入 pandas DataFrame

        Insert rows into pandas DataFrame while maintaining column data types(在维护列数据类型的同时将行插入 pandas DataFrame)
        <i id='RY42g'><tr id='RY42g'><dt id='RY42g'><q id='RY42g'><span id='RY42g'><b id='RY42g'><form id='RY42g'><ins id='RY42g'></ins><ul id='RY42g'></ul><sub id='RY42g'></sub></form><legend id='RY42g'></legend><bdo id='RY42g'><pre id='RY42g'><center id='RY42g'></center></pre></bdo></b><th id='RY42g'></th></span></q></dt></tr></i><div id='RY42g'><tfoot id='RY42g'></tfoot><dl id='RY42g'><fieldset id='RY42g'></fieldset></dl></div>

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

          • <tfoot id='RY42g'></tfoot>
              <tbody id='RY42g'></tbody>

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

              <bdo id='RY42g'></bdo><ul id='RY42g'></ul>

                1. 本文介绍了在维护列数据类型的同时将行插入 pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在保持列数据类型的同时,将新行插入现有 pandas DataFrame 的最佳方法是什么,同时为未指定的列提供用户定义的填充值?这是一个例子:

                  What's the best way to insert new rows into an existing pandas DataFrame while maintaining column data types and, at the same time, giving user-defined fill values for columns that aren't specified? Here's an example:

                  df = pd.DataFrame({
                      'name': ['Bob', 'Sue', 'Tom'],
                      'age': [45, 40, 10],
                      'weight': [143.2, 130.2, 34.9],
                      'has_children': [True, True, False]
                  })
                  

                  假设我想添加一条只传递 nameage 的新记录.为了维护数据类型,我可以从 df 复制行,修改值,然后将 df 附加到副本,例如

                  Assume that I want to add a new record passing just name and age. To maintain data types, I can copy rows from df, modify values and then append df to the copy, e.g.

                  columns = ('name', 'age')
                  copy_df = df.loc[0:0, columns].copy()
                  copy_df.loc[0, columns] = 'Cindy', 42
                  new_df = copy_df.append(df, sort=False).reset_index(drop=True)
                  

                  但这会将 bool 列转换为对象.

                  But that converts the bool column to an object.

                  这是一个非常老套的解决方案,感觉不是这样做的正确方法":

                  Here's a really hacky solution that doesn't feel like the "right way" to do this:

                  columns = ('name', 'age')
                  copy_df = df.loc[0:0].copy()
                  
                  missing_remap = {
                      'int64': 0,
                      'float64': 0.0,
                      'bool': False,
                      'object': ''
                  }
                  for c in set(copy_df.columns).difference(columns)):
                      copy_df.loc[:, c] = missing_remap[str(copy_df[c].dtype)]
                  
                  new_df = copy_df.append(df, sort=False).reset_index(drop=True)
                  new_df.loc[0, columns] = 'Cindy', 42
                  

                  我知道我一定错过了什么.

                  I know I must be missing something.

                  推荐答案

                  如你所见,由于 NaNfloat,添加 NaN到一个系列可能会导致它被向上转换为 float 或转换为 object.您确定这不是一个理想的结果是正确的.

                  As you found, since NaN is a float, adding NaN to a series may cause it to be either upcasted to float or converted to object. You are right in determining this is not a desirable outcome.

                  没有直接的方法.我的建议是将您的输入行数据存储在字典中,并在附加之前将其与默认字典相结合.请注意,这是有效的,因为 pd.DataFrame.append 接受 dict 参数.

                  There is no straightforward approach. My suggestion is to store your input row data in a dictionary and combine it with a dictionary of defaults before appending. Note that this works because pd.DataFrame.append accepts a dict argument.

                  在 Python 3.6 中,您可以使用语法 {**d1, **d2} 组合两个字典,并优先选择第二个.

                  In Python 3.6, you can use the syntax {**d1, **d2} to combine two dictionaries with preference for the second.

                  default = {'name': '', 'age': 0, 'weight': 0.0, 'has_children': False}
                  
                  row = {'name': 'Cindy', 'age': 42}
                  
                  df = df.append({**default, **row}, ignore_index=True)
                  
                  print(df)
                  
                     age  has_children   name  weight
                  0   45          True    Bob   143.2
                  1   40          True    Sue   130.2
                  2   10         False    Tom    34.9
                  3   42         False  Cindy     0.0
                  
                  print(df.dtypes)
                  
                  age               int64
                  has_children       bool
                  name             object
                  weight          float64
                  dtype: object
                  

                  这篇关于在维护列数据类型的同时将行插入 pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                    • <tfoot id='axlfp'></tfoot>

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

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