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

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

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

      1. 为 pandas.read_csv 指定正确的 dtypes 以获取日期时间

        时间:2023-08-04

          • <bdo id='osQNc'></bdo><ul id='osQNc'></ul>
              <tbody id='osQNc'></tbody>
          • <small id='osQNc'></small><noframes id='osQNc'>

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

                  <legend id='osQNc'><style id='osQNc'><dir id='osQNc'><q id='osQNc'></q></dir></style></legend>
                1. 本文介绍了为 pandas.read_csv 指定正确的 dtypes 以获取日期时间和布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在将 csv 文件加载到 Pandas DataFrame 中.对于每一列,如何使用 dtype 参数指定它包含的数据类型?

                  I am loading a csv file into a Pandas DataFrame. For each column, how do I specify what type of data it contains using the dtype argument?

                  • 我可以使用 numeric 数据(代码在底部)...
                  • 但是如何指定时间数据...
                  • 分类数据,例如因子或布尔值?我试过 np.bool_pd.tslib.Timestamp 没有运气.
                  • I can do it with numeric data (code at bottom)...
                  • But how do I specify time data...
                  • and categorical data such as factors or booleans? I have tried np.bool_ and pd.tslib.Timestamp without luck.

                  代码:

                  import pandas as pd
                  import numpy as np
                  df = pd.read_csv(<file-name>, dtype={'A': np.int64, 'B': np.float64})
                  

                  推荐答案

                  read_csv 有很多选项可以处理你提到的所有情况.您可能想尝试 dtype={'A': datetime.datetime},但通常您不需要 dtypes,因为 pandas 可以推断类型.

                  There are a lot of options for read_csv which will handle all the cases you mentioned. You might want to try dtype={'A': datetime.datetime}, but often you won't need dtypes as pandas can infer the types.

                  对于日期,则需要指定 parse_date 选项:

                  parse_dates : boolean, list of ints or names, list of lists, or dict
                  keep_date_col : boolean, default False
                  date_parser : function
                  

                  一般来说,要转换布尔值,您需要指定:

                  true_values  : list  Values to consider as True
                  false_values : list  Values to consider as False
                  

                  这会将列表中的任何值转换为布尔值 true/false.对于更一般的转换,您很可能需要

                  Which will transform any value in the list to the boolean true/false. For more general conversions you will most likely need

                  转换器:字典.用于转换某些列中的值的可选函数字典.键可以是整数或列标签

                  converters : dict. optional Dict of functions for converting values in certain columns. Keys can either be integers or column labels

                  虽然密集,但请在此处查看完整列表:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html

                  Though dense, check here for the full list: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html

                  这篇关于为 pandas.read_csv 指定正确的 dtypes 以获取日期时间和布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从数据框中获取价值 下一篇:将 pandas.DataFrame 转换为字节

                  相关文章

                  最新文章

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

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

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

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