• <small id='g1Bvd'></small><noframes id='g1Bvd'>

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

      <tfoot id='g1Bvd'></tfoot>

      1. pandas 数据框将列类型转换为字符串或分类

        时间:2023-08-04
            <bdo id='XXmo6'></bdo><ul id='XXmo6'></ul>
              <tbody id='XXmo6'></tbody>
              • <i id='XXmo6'><tr id='XXmo6'><dt id='XXmo6'><q id='XXmo6'><span id='XXmo6'><b id='XXmo6'><form id='XXmo6'><ins id='XXmo6'></ins><ul id='XXmo6'></ul><sub id='XXmo6'></sub></form><legend id='XXmo6'></legend><bdo id='XXmo6'><pre id='XXmo6'><center id='XXmo6'></center></pre></bdo></b><th id='XXmo6'></th></span></q></dt></tr></i><div id='XXmo6'><tfoot id='XXmo6'></tfoot><dl id='XXmo6'><fieldset id='XXmo6'></fieldset></dl></div>
                <tfoot id='XXmo6'></tfoot>

              • <small id='XXmo6'></small><noframes id='XXmo6'>

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

                1. 本文介绍了 pandas 数据框将列类型转换为字符串或分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何将 pandas 数据框的单列转换为字符串类型?在下面的住房数据 df 中,我需要将邮政编码转换为字符串,以便在运行线性回归时,邮政编码被视为分类而不是数字.谢谢!

                  How do I convert a single column of a pandas dataframe to type string? In the df of housing data below I need to convert zipcode to string so that when I run linear regression, zipcode is treated as categorical and not numeric. Thanks!

                  df = pd.DataFrame({'zipcode': {17384: 98125, 2680: 98107, 722: 98005, 18754: 98109, 14554: 98155}, 'bathrooms': {17384: 1.5, 2680: 0.75, 722: 3.25, 18754: 1.0, 14554: 2.5}, 'sqft_lot': {17384: 1650, 2680: 3700, 722: 51836, 18754: 2640, 14554: 9603}, 'bedrooms': {17384: 2, 2680: 2, 722: 4, 18754: 2, 14554: 4}, 'sqft_living': {17384: 1430, 2680: 1440, 722: 4670, 18754: 1130, 14554: 3180}, 'floors': {17384: 3.0, 2680: 1.0, 722: 2.0, 18754: 1.0, 14554: 2.0}})
                  print (df)
                         bathrooms  bedrooms  floors  sqft_living  sqft_lot  zipcode
                  722         3.25         4     2.0         4670     51836    98005
                  2680        0.75         2     1.0         1440      3700    98107
                  14554       2.50         4     2.0         3180      9603    98155
                  17384       1.50         2     3.0         1430      1650    98125
                  18754       1.00         2     1.0         1130      2640    98109
                  

                  推荐答案

                  你需要astype:

                  df['zipcode'] = df.zipcode.astype(str)
                  #df.zipcode = df.zipcode.astype(str)
                  

                  <小时>

                  用于转换为分类:

                  df['zipcode'] = df.zipcode.astype('category')
                  #df.zipcode = df.zipcode.astype('category')
                  

                  另一种解决方案是分类:

                  Another solution is Categorical:

                  df['zipcode'] = pd.Categorical(df.zipcode)
                  

                  数据样本:

                  import pandas as pd
                  
                  df = pd.DataFrame({'zipcode': {17384: 98125, 2680: 98107, 722: 98005, 18754: 98109, 14554: 98155}, 'bathrooms': {17384: 1.5, 2680: 0.75, 722: 3.25, 18754: 1.0, 14554: 2.5}, 'sqft_lot': {17384: 1650, 2680: 3700, 722: 51836, 18754: 2640, 14554: 9603}, 'bedrooms': {17384: 2, 2680: 2, 722: 4, 18754: 2, 14554: 4}, 'sqft_living': {17384: 1430, 2680: 1440, 722: 4670, 18754: 1130, 14554: 3180}, 'floors': {17384: 3.0, 2680: 1.0, 722: 2.0, 18754: 1.0, 14554: 2.0}})
                  

                  print (df)
                         bathrooms  bedrooms  floors  sqft_living  sqft_lot  zipcode
                  722         3.25         4     2.0         4670     51836    98005
                  2680        0.75         2     1.0         1440      3700    98107
                  14554       2.50         4     2.0         3180      9603    98155
                  17384       1.50         2     3.0         1430      1650    98125
                  18754       1.00         2     1.0         1130      2640    98109
                  
                  print (df.dtypes)
                  bathrooms      float64
                  bedrooms         int64
                  floors         float64
                  sqft_living      int64
                  sqft_lot         int64
                  zipcode          int64
                  dtype: object
                  
                  df['zipcode'] = df.zipcode.astype('category')
                  
                  print (df)
                         bathrooms  bedrooms  floors  sqft_living  sqft_lot zipcode
                  722         3.25         4     2.0         4670     51836   98005
                  2680        0.75         2     1.0         1440      3700   98107
                  14554       2.50         4     2.0         3180      9603   98155
                  17384       1.50         2     3.0         1430      1650   98125
                  18754       1.00         2     1.0         1130      2640   98109
                  
                  print (df.dtypes)
                  bathrooms       float64
                  bedrooms          int64
                  floors          float64
                  sqft_living       int64
                  sqft_lot          int64
                  zipcode        category
                  dtype: object
                  

                  这篇关于 pandas 数据框将列类型转换为字符串或分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 python3 中将 OrderedDict 转换为常规字典 下一篇:使用 Pandas 将整个数据帧从小写转换为大写

                  相关文章

                  最新文章

                  <tfoot id='VN35s'></tfoot>

                  1. <legend id='VN35s'><style id='VN35s'><dir id='VN35s'><q id='VN35s'></q></dir></style></legend>
                      <bdo id='VN35s'></bdo><ul id='VN35s'></ul>

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

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