【问题标题】:Multiply every 2nd row by -1 in pandas col在 pandas col 中将每第二行乘以 -1
【发布时间】:2021-05-19 01:24:24
【问题描述】:

我试图将每第二行乘以 -1,但仅在指定的列中。使用下面,我希望将c 列中的每第二行乘以 -1。

df = pd.DataFrame({  
    'a' : [2.0,1.0,3.5,2.0,5.0,3.0,1.0,1.0],
    'b' : [1.0,-1.0,3.5,3.0,4.0,2.0,3.0,2.0],     
    'c' : [2.0,2.0,2.0,2.0,-1.0,-1.0,-2.0,-2.0],                  
    })

df['c'] = df['c'][::2] * -1

预期输出:

     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0

【问题讨论】:

  • 这个线程中当前的最佳答案是对我链接的那个线程中的技术进行了简单的修改。
  • @Chopin - 不,这不是骗人的。如果关闭,请告诉我重新打开。
  • @blorgon,认为你在修饰一点点。
  • 肖邦是正确的,这不是完全重复的,应该保持打开状态。 @blorgon:缺少的部分是 jezrael 显示的 [1::2]odd-row -onlyindexing。

标签: python pandas


【解决方案1】:

一种使用pandas.DataFrame.update的方式:

df.update(df['c'][1::2] * -1)
print(df)

输出:

     a    b    c
0  2.0  1.0  2.0
1  1.0 -1.0 -2.0
2  3.5  3.5  2.0
3  2.0  3.0 -2.0
4  5.0  4.0 -1.0
5  3.0  2.0  1.0
6  1.0  3.0 -2.0
7  1.0  2.0  2.0

【讨论】:

    【解决方案2】:

    使用DataFrame.iloc 进行切片,Index.get_loc 用于列的位置c

    df.iloc[1::2, df.columns.get_loc('c')] *= -1
    #working same like
    #df.iloc[1::2, df.columns.get_loc('c')] = df.iloc[1::2, df.columns.get_loc('c')] * -1
    

    或者将DataFrame.locdf.index 中的选择值一起使用:

    df.loc[df.index[1::2], 'c'] *= -1
    

    或者:

    df.loc[df.index % 2 == 1, 'c'] *= -1
    

    print (df)
        
         a    b    c
    0  2.0  1.0  2.0
    1  1.0 -1.0 -2.0
    2  3.5  3.5  2.0
    3  2.0  3.0 -2.0
    4  5.0  4.0 -1.0
    5  3.0  2.0  1.0
    6  1.0  3.0 -2.0
    7  1.0  2.0  2.0
    

    【讨论】:

      【解决方案3】:

      或者你可以编写自己的函数:

      def multiple(df):
          new_df = pd.DataFrame()
          for i in range(0, len(df)):
              if i // 2 == 0:
                  new_row = pd.DataFrame(data = df.iloc[i]*(-1)).T      
                  new_df = new_df.append(new_row, ignore_index=True)
              else:
                  new_row = pd.DataFrame(data = df.iloc[i]).T      
                  new_df = new_df.append(new_row, ignore_index=True)            
      
              i+=1
              
          return new_df
      

      【讨论】:

        【解决方案4】:

        您可以使用此代码:

             a    b    c
        0  2.0  1.0  2.0
        1  1.0 -1.0  2.0
        2  3.5  3.5  2.0
        3  2.0  3.0  2.0
        4  5.0  4.0 -1.0
        5  3.0  2.0 -1.0
        6  1.0  3.0 -2.0
        7  1.0  2.0 -2.0
        
        df.loc[df.index % 2 == 1, "c" ] = df.c * - 1
        
             a    b    c
        0  2.0  1.0  2.0
        1  1.0 -1.0 -2.0
        2  3.5  3.5  2.0
        3  2.0  3.0 -2.0
        4  5.0  4.0 -1.0
        5  3.0  2.0  1.0
        6  1.0  3.0 -2.0
        7  1.0  2.0  2.0
        

        【讨论】:

          【解决方案5】:

          您可以将 divmod 与系列一起使用:

          s = 2*np.arange(len(df))%2 - 1
          df["c"] = -df.c*s
          
               a    b    c
          0  2.0  1.0  2.0
          1  1.0 -1.0 -2.0
          2  3.5  3.5  2.0
          3  2.0  3.0 -2.0
          4  5.0  4.0 -1.0
          5  3.0  2.0  1.0
          6  1.0  3.0 -2.0
          7  1.0  2.0  2.0
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-17
            • 2022-08-02
            • 2020-05-03
            • 2013-06-16
            • 2021-06-05
            • 1970-01-01
            • 1970-01-01
            • 2020-05-30
            相关资源
            最近更新 更多