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

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

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

      <tfoot id='tHUGn'></tfoot>
      <i id='tHUGn'><tr id='tHUGn'><dt id='tHUGn'><q id='tHUGn'><span id='tHUGn'><b id='tHUGn'><form id='tHUGn'><ins id='tHUGn'></ins><ul id='tHUGn'></ul><sub id='tHUGn'></sub></form><legend id='tHUGn'></legend><bdo id='tHUGn'><pre id='tHUGn'><center id='tHUGn'></center></pre></bdo></b><th id='tHUGn'></th></span></q></dt></tr></i><div id='tHUGn'><tfoot id='tHUGn'></tfoot><dl id='tHUGn'><fieldset id='tHUGn'></fieldset></dl></div>
    1. 如何对具有重复列名行进行切片,并按顺序堆叠这些行

      How to slice a row with duplicate column names and stack that rows in order(如何对具有重复列名行进行切片,并按顺序堆叠这些行)

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

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

            <bdo id='gAase'></bdo><ul id='gAase'></ul>
          • <tfoot id='gAase'></tfoot>

                  <tbody id='gAase'></tbody>
                本文介绍了如何对具有重复列名行进行切片,并按顺序堆叠这些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个如图所示的数据帧,我希望在不更改顺序的情况下将其转换为多行。

                  RESP HR SPO2 PULSE
                1  46  122  0    0   
                2  46  122  0    0   
                3
                4
                

                推荐答案

                一种可能解决方案是使用reshape,仅需要的列长模数为0(因此可以将所有数据转换为4列DataFrame):

                df1 = pd.Dataframe(df.values.reshape(-1, 4), columns=['RESP','HR','SPO2','PULSE'])
                df1['RESP1'] = df['RESP'].shift(-1)
                

                通用数据解决方案:

                a = '46 122 0 0 46 122 0 0 45 122 0 0 45 122 0'.split()
                df = pd.DataFrame([a]).astype(int)
                print (df)
                    0    1  2  3   4    5  6  7   8    9  10  11  12   13  14
                0  46  122  0  0  46  122  0  0  45  122   0   0  45  122   0
                
                #flatten values
                a = df.values.ravel()
                #number of new columns
                N = 4
                #array filled by NaNs for possible add NaNs to end of last row
                arr = np.full(((len(a) - 1)//N + 1)*N, np.nan)
                #fill array by flatten values
                arr[:len(a)] = a
                #reshape to new DataFrame (last value is NaN)
                df1 = pd.DataFrame(arr.reshape((-1, N)), columns=['RESP','HR','SPO2','PULSE'])
                #new column with shifting first col
                df1['RESP1'] = df1['RESP'].shift(-1)
                print(df1)
                   RESP     HR  SPO2  PULSE  RESP1
                0  46.0  122.0   0.0    0.0   46.0
                1  46.0  122.0   0.0    0.0   45.0
                2  45.0  122.0   0.0    0.0   45.0
                3  45.0  122.0   0.0    NaN    NaN
                

                这篇关于如何对具有重复列名行进行切片,并按顺序堆叠这些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)

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

                  <tfoot id='0fdih'></tfoot>

                    <small id='0fdih'></small><noframes id='0fdih'>

                          <bdo id='0fdih'></bdo><ul id='0fdih'></ul>
                            <tbody id='0fdih'></tbody>
                        • <legend id='0fdih'><style id='0fdih'><dir id='0fdih'><q id='0fdih'></q></dir></style></legend>