<bdo id='36vOI'></bdo><ul id='36vOI'></ul>

        <tfoot id='36vOI'></tfoot>

        <small id='36vOI'></small><noframes id='36vOI'>

      1. <i id='36vOI'><tr id='36vOI'><dt id='36vOI'><q id='36vOI'><span id='36vOI'><b id='36vOI'><form id='36vOI'><ins id='36vOI'></ins><ul id='36vOI'></ul><sub id='36vOI'></sub></form><legend id='36vOI'></legend><bdo id='36vOI'><pre id='36vOI'><center id='36vOI'></center></pre></bdo></b><th id='36vOI'></th></span></q></dt></tr></i><div id='36vOI'><tfoot id='36vOI'></tfoot><dl id='36vOI'><fieldset id='36vOI'></fieldset></dl></div>
        <legend id='36vOI'><style id='36vOI'><dir id='36vOI'><q id='36vOI'></q></dir></style></legend>
      2. 使用 Pandas 将整个数据帧从小写转换为大写

        时间:2023-08-04

              <tfoot id='MWQxM'></tfoot>

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

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

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

                  本文介绍了使用 Pandas 将整个数据帧从小写转换为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个如下所示的数据框:

                  I have a dataframe like the one displayed below:

                  # Create an example dataframe about a fictional army
                  raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks'],
                              'company': ['1st', '1st', '2nd', '2nd'],
                              'deaths': ['kkk', 52, '25', 616],
                              'battles': [5, '42', 2, 2],
                              'size': ['l', 'll', 'l', 'm']}
                  df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size'])
                  

                  我的目标是将数据框中的每个字符串都转换为大写,使其看起来像这样:

                  My goal is to transform every single string inside of the dataframe to upper case so that it looks like this:

                  注意:所有数据类型均为对象,不得更改;输出必须包含所有对象.我想避免将每一列一一转换...我想一般在整个数据帧上进行.

                  Notice: all data types are objects and must not be changed; the output must contain all objects. I want to avoid to convert every single column one by one... I would like to do it generally over the whole dataframe possibly.

                  到目前为止我尝试的是这样做但没有成功

                  What I tried so far is to do this but without success

                  df.str.upper()
                  

                  推荐答案

                  astype() 会将每个系列转换为 dtype 对象(字符串),然后调用 str() 方法在转换后的系列上从字面上获取字符串并调用函数 upper() 就可以了.请注意,在此之后,所有列的 dtype 都会更改为 object.

                  astype() will cast each series to the dtype object (string) and then call the str() method on the converted series to get the string literally and call the function upper() on it. Note that after this, the dtype of all columns changes to object.

                  In [17]: df
                  Out[17]: 
                       regiment company deaths battles size
                  0  Nighthawks     1st    kkk       5    l
                  1  Nighthawks     1st     52      42   ll
                  2  Nighthawks     2nd     25       2    l
                  3  Nighthawks     2nd    616       2    m
                  
                  In [18]: df.apply(lambda x: x.astype(str).str.upper())
                  Out[18]: 
                       regiment company deaths battles size
                  0  NIGHTHAWKS     1ST    KKK       5    L
                  1  NIGHTHAWKS     1ST     52      42   LL
                  2  NIGHTHAWKS     2ND     25       2    L
                  3  NIGHTHAWKS     2ND    616       2    M
                  

                  您可以稍后使用 to_numeric():

                  In [42]: df2 = df.apply(lambda x: x.astype(str).str.upper())
                  
                  In [43]: df2['battles'] = pd.to_numeric(df2['battles'])
                  
                  In [44]: df2
                  Out[44]: 
                       regiment company deaths  battles size
                  0  NIGHTHAWKS     1ST    KKK        5    L
                  1  NIGHTHAWKS     1ST     52       42   LL
                  2  NIGHTHAWKS     2ND     25        2    L
                  3  NIGHTHAWKS     2ND    616        2    M
                  
                  In [45]: df2.dtypes
                  Out[45]: 
                  regiment    object
                  company     object
                  deaths      object
                  battles      int64
                  size        object
                  dtype: object
                  

                  这篇关于使用 Pandas 将整个数据帧从小写转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:pandas 数据框将列类型转换为字符串或分类 下一篇:Python,确定字符串是否应转换为 Int 或 Float

                  相关文章

                  最新文章

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

                      <tfoot id='MdCjh'></tfoot>

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

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

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