Python中的乘法运算和字符串拼接

3

我有一个Pandas数据框

   Colour   Type    Cost    Price   Type
0  Red      Car     3        5       Standard
1  Blue     Bike    6        7       Standard
2  Blue     Car     4        8       Standard
3  Green    Bike    6        9       Standard
4   Yellow  Bike    3        3       Standard

我有一系列的调整数据,想要将成本乘以这些数据并添加到底部。

Red   2
Blue  1
Green 3

因此,输出结果为:
   Colour   Type    Cost    Price   Type
0  Red      Car     3        5       Standard
1  Blue     Bike    6        7       Standard
2  Blue     Car     4        8       Standard
3  Green    Bike    6        9       Standard
4   Yellow  Bike    3        3       Standard
0  Red      Car     6        10       Adjusted
1  Blue     Bike    6        7       Adjusted
2  Blue     Car     4        8       Adjusted
3  Green    Bike    18        27       Adjusted

我有点迷茫,是否有更简单的方法来完成这个操作?


在第二个数据框中,列的名称也被称为颜色成本吗? - Finn
好的,第二个数据框实际上只是一个系列。我会进行修改。 - fred.schwartz
5个回答

2
你可以使用“merge”和“concat”方法。调整数据框:
   Colour   value
0   Red     2
1   Blue    1
2   Green   3

然后:

temp = df.merge(adj)

temp["Cost"] = temp["Cost"]*temp["value"]
temp["Price"] = temp["Price"]*temp["value"]
temp["Type.1"] = ["Adjusted"]*temp.shape[0]


pd.concat([df, temp.iloc[:,0:5]], axis=0)

输出:

    Colour  Type    Cost    Price   Type.1
0   Red     Car     3   5   Standard
1   Blue    Bike    6   7   Standard
2   Blue    Car     4   8   Standard
3   Green   Bike    6   9   Standard
4   Yellow  Bike    3   3   Standard
0   Red     Car     6   10  Adjusted
1   Blue    Bike    6   7   Adjusted
2   Blue    Car     4   8   Adjusted
3   Green   Bike    18  27  Adjusted

2

为了增加一些变化,以下是现有解决方案的补充:

假设调整系列如下所示:s

s = pd.Series({'Red': 2, 'Blue': 1, 'Green': 3})

我们也可以对价格列进行堆叠(stack)并相乘,然后再撤销堆叠(unstack),最后进行连接(concat):

m = (df1.set_index(['Colour','Type'],append=True)[['Cost','Price']].stack()
        .mul(s,level=1).dropna().unstack().reset_index(['Colour','Type']))

pd.concat((df1,m),sort=False).fillna({'Type.1':'Adjusted'})

   Colour  Type  Cost  Price    Type.1
0     Red   Car   3.0    5.0  Standard
1    Blue  Bike   6.0    7.0  Standard
2    Blue   Car   4.0    8.0  Standard
3   Green  Bike   6.0    9.0  Standard
4  Yellow  Bike   3.0    3.0  Standard
0     Red   Car   6.0   10.0  Adjusted
1    Blue  Bike   6.0    7.0  Adjusted
2    Blue   Car   4.0    8.0  Adjusted
3   Green  Bike  18.0   27.0  Adjusted

1

据我所知,“assign”、“map”、“join”和“concat”

请注意,我已将您的第二个Type列小写,以避免出现.1标记。

data = {'Red' :   2,
'Blue' : 1,
'Green' : 3}

new_df = pd.concat(
    [
        df,
        df[["Colour", "Type"]]
        .join(
            df.select_dtypes("int64")
            .mul(df["Colour"].map(data), axis=0)
            .assign(type="adjusted")
        )
        .dropna(),
    ]
)

print(new_df)

   Colour  Type  Cost  Price      type
0     Red   Car   3.0    5.0  Standard
1    Blue  Bike   6.0    7.0  Standard
2    Blue   Car   4.0    8.0  Standard
3   Green  Bike   6.0    9.0  Standard
4  Yellow  Bike   3.0    3.0  Standard
0     Red   Car   6.0   10.0  adjusted
1    Blue  Bike   6.0    7.0  adjusted
2    Blue   Car   4.0    8.0  adjusted
3   Green  Bike  18.0   27.0  adjusted

1
使用 df.mergedf.append
In [2349]: df1 
Out[2349]: 
   Colour  Type  Cost  Price    Type.1
0     Red   Car     3      5  Standard
1    Blue  Bike     6      7  Standard
2    Blue   Car     4      8  Standard
3   Green  Bike     6      9  Standard
4  Yellow  Bike     3      3  Standard

In [2350]: df2  
Out[2350]: 
  Colour  Price
0    Red      2
1   Blue      1
2  Green      3

In [2341]: res = df1.merge(df2, on='Colour') 

In [2343]: res['Price'] = res.Price_x * res.Price_y    
In [2344]: res['Type.1'] = 'Adjusted'

In [2346]: res.drop(['Price_x','Price_y'], 1, inplace=True)

In [2351]: df1 = df1.append(res)

In [2352]: df1
Out[2352]: 
   Colour  Type  Cost  Price    Type.1
0     Red   Car     3      5  Standard
1    Blue  Bike     6      7  Standard
2    Blue   Car     4      8  Standard
3   Green  Bike     6      9  Standard
4  Yellow  Bike     3      3  Standard
0     Red   Car     3     10  Adjusted
1    Blue  Bike     6      7  Adjusted
2    Blue   Car     4      8  Adjusted
3   Green  Bike     6     27  Adjusted

1
你可以使用df.merge,然后使用df.mulpd.concat
df
#   Colour  Type  Cost  Price    Type.1
#0     Red   Car     3      5  Standard
#1    Blue  Bike     6      7  Standard
#2    Blue   Car     4      8  Standard
#3   Green  Bike     6      9  Standard
#4  Yellow  Bike     3      3  Standard
dfs
#  Colour  values
#0    Red       2
#1   Blue       1
#2  Green       3

dfm = dfs.merge(df, on='Colour')
dfm[['Cost','Price']] = dfm[['Cost', 'Price']].mul(dfm['values'] ,axis=0)
dfm['Type.1'] = 'Adjusted'

pd.concat([df,dfm.loc[:,df.columns]])

   Colour  Type  Cost  Price    Type.1
0     Red   Car     3      5  Standard
1    Blue  Bike     6      7  Standard
2    Blue   Car     4      8  Standard
3   Green  Bike     6      9  Standard
4  Yellow  Bike     3      3  Standard
0     Red   Car     6     10  Adjusted
1    Blue  Bike     6      7  Adjusted
2    Blue   Car     4      8  Adjusted
3   Green  Bike    18     27  Adjusted

如果您拥有的映射是一个索引为Colour的系列,则使用pd.Series.mapdf.select_dtypesdf._get_numeric_data私有方法
dfs # Type `pandas.core.series.Series`
#Colour
#Red      2
#Blue     1
#Green    3
#Name: values, dtype: int64

t = df.copy()
cols = t.select_dtypes(np.int64).columns
t[cols] = t[cols].mul(t['Colour'].map(dfs),axis=0)
t['Type.1'] = 'Adjusted'
pd.concat([df,t.dropna()])

   Colour  Type  Cost  Price    Type.1
0     Red   Car   3.0    5.0  Standard
1    Blue  Bike   6.0    7.0  Standard
2    Blue   Car   4.0    8.0  Standard
3   Green  Bike   6.0    9.0  Standard
4  Yellow  Bike   3.0    3.0  Standard
0     Red   Car   6.0   10.0  Adjusted
1    Blue  Bike   6.0    7.0  Adjusted
2    Blue   Car   4.0    8.0  Adjusted
3   Green  Bike  18.0   27.0  Adjusted

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接