【问题标题】:Moving specific strings between columns in pandas在熊猫的列之间移动特定字符串
【发布时间】:2023-02-05 03:43:13
【问题描述】:

我有一个熊猫属性数据框。

**Address**      | **Added on**.       | 
15 Smith Close   |  Added on 17/11/22  |
1 Apple Drive    |  Reduced on 19/11/22|
27 Pride place   |  Added on 18/1//22  |

我想将“Added on”列中所有“reduced on ...”的实例移动到数据框中名为“Reduced on”的另一列。我该怎么做?

非常感谢。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你可以使用pd.DataFrame.where

    df['Reduced on'] = df['Added on'].where(df['Added on'].str.contains('Reduced on'))
    df['Added on'] = df['Added on'].where(~ df['Added on'].str.contains('Reduced on'))
    
    df
    
              Address           Added on           Reduced on
    0  15 Smith Close  Added on 17/11/22                  NaN
    1   1 Apple Drive                NaN  Reduced on 19/11/22
    2  27 Pride place  Added on 18/1//22                  NaN
    

    或者另一种是使用pd.Series.str.extract & pd.DataFrame.concat

    pd.concat([df['Address'], df['Added on'].str.extract('(?P<Added_on>Add.*)|(?P<Reduced_on>Reduced.*)')], axis=1)
    
              Address           Added_on           Reduced_on
    0  15 Smith Close  Added on 17/11/22                  NaN
    1   1 Apple Drive                NaN  Reduced on 19/11/22
    2  27 Pride place  Added on 18/1//22                  NaN
    

    【讨论】:

    • 谢谢阿努希拉万。就这样排序了。
    • 哈哈,我刚刚注意到 Anoushiravan 的编码方式与我类似。即使 Anoushiravan 是最快的,我也会让我的代码到位
    • @LaurentB。是的,只要保留它。它经常发生。
    【解决方案2】:

    建议代码:

    import pandas as pd
    import numpy as np
    
    # Build Dataframe to work on
    df = pd.DataFrame({"**Address** ": ['15 Smith Close' , '1 Apple Drive', '27 Pride place'], 
                       "**Added on**": ['Added on 17/11/22', 'Reduced on 19/11/22', 'Added on 18/1//22']})
    
    # Define the mask m
    m = df['**Added on**'].str.contains('Reduced')               
    
    # 1- Move 'Reduced' rows to **New Col**                       
    df['**Reduced on**'] = df['**Added on**'].where(m, np.nan)
    # 2- Erase 'Reduced' rows from  **Added on**
    df['**Added on**'] = df['**Added on**'].where(~m, np.nan) 
    
    print(df)
    

    结果 :

         **Address**        **Added on**          **Reduced on**
    0  15 Smith Close  Added on 17/11/22                  NaN
    1   1 Apple Drive                NaN  Reduced on 19/11/22
    2  27 Pride place  Added on 18/1//22                  NaN
    
    

    【讨论】:

      【解决方案3】:

      这也应该有效:

      (df[['Address']].join(df[['Added on']]
      .set_index(df['Added on']
      .str.rsplit(n=1)
      .str[0]
      .rename(None),append=True)['Added on']
      .unstack()))
      

      或者

      df['Added on'].str.rsplit(' ',n=1).str[0].str.get_dummies().mul(df['Added on'],axis=0)
      

      输出:

                Address           Added on           Reduced on
      0  15 Smith Close  Added on 17/11/22                  NaN
      1   1 Apple Drive                NaN  Reduced on 19/11/22
      2  27 Pride place  Added on 18/1//22                  NaN
      

      【讨论】:

        猜你喜欢
        • 2017-04-29
        • 2022-11-02
        • 2022-07-20
        • 2017-11-28
        • 2021-07-03
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多