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

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

      1. 在 pandas 数据框中合并两列文本

        Combine two columns of text in pandas dataframe(在 pandas 数据框中合并两列文本)

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

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

              <tfoot id='qlnea'></tfoot>
            • <legend id='qlnea'><style id='qlnea'><dir id='qlnea'><q id='qlnea'></q></dir></style></legend>
                <tbody id='qlnea'></tbody>

                  本文介绍了在 pandas 数据框中合并两列文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a 20 x 4000 dataframe in Python using pandas. Two of these columns are named Year and quarter. I'd like to create a variable called period that makes Year = 2000 and quarter= q2 into 2000q2.

                  Can anyone help with that?

                  解决方案

                  If both columns are strings, you can concatenate them directly:

                  df["period"] = df["Year"] + df["quarter"]
                  

                  If one (or both) of the columns are not string typed, you should convert it (them) first,

                  df["period"] = df["Year"].astype(str) + df["quarter"]
                  

                  Beware of NaNs when doing this!


                  If you need to join multiple string columns, you can use agg:

                  df['period'] = df[['Year', 'quarter', ...]].agg('-'.join, axis=1)
                  

                  Where "-" is the separator.

                  这篇关于在 pandas 数据框中合并两列文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                    <tbody id='9naWV'></tbody>
                    <legend id='9naWV'><style id='9naWV'><dir id='9naWV'><q id='9naWV'></q></dir></style></legend>

                      • <bdo id='9naWV'></bdo><ul id='9naWV'></ul>

                          <small id='9naWV'></small><noframes id='9naWV'>

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