本文介绍了条件pandas dataframe python复制行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框有问题.

我的df是:

product      power                   brand
product_1    3 x 1500W               brand_A
product_2    2x1000W + 1x100W
product 3    1x1500W + 1x500W        brand_B
product 4    500W

我需要将每行乘以乘积的乘积(用幂扣除)

I need to mulpliy each rows with number of product (deducted with power)

我的df预期:

product      power               brand          new_product
product_1    1500W               brand_A        product_1_1
product_1    1500W               brand_A        product_1_2
product_1    1500W               brand_A        product_1_3
product_2    1000W                              product_2_1
product_2    1000W                              product_2_2
product_2    100W                               product_2_3
product 3    1500W               brand_B        product_3_1
product 3    500W                brand_B        product_3_2
product 4    500W                               product_4_1

感谢您的帮助

推荐答案

我将进行字符串提取和合并,然后执行一些清理任务:

I would do a string extract and merge, followed by some cleaning tasks:

df1 = (df.power.str.extractall('(\d+)\s?x\s?(\d+W)')
         .reset_index(level=1,drop=True)
      )

new_df = df.merge(df1[1].repeat(df1[0]),
                  left_index=True,
                  right_index=True,
                  how='outer')

# update the power column
new_df['power']= np.where(new_df[1].isna(), new_df['power'], new_df[1])

# drop the extra 1 column
new_df.drop(1, axis=1, inplace=True)

# new_product column
new_df['new_product'] = (new_df['product'] + '_' +
                         new_df.groupby('product').cumcount().add(1).astype(str) )

输出:

     product  power    brand  new_product
0  product_1  1500W  brand_A  product_1_1
0  product_1  1500W  brand_A  product_1_2
0  product_1  1500W  brand_A  product_1_3
1  product_2  1000W     None  product_2_1
1  product_2  1000W     None  product_2_2
1  product_2   100W     None  product_2_3
2  product 3  1500W  brand_B  product 3_1
2  product 3   500W  brand_B  product 3_2
3  product 4   500W     None  product 4_1

这篇关于条件pandas dataframe python复制行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-08 22:41