简体   繁体   English

将每个值乘以两个数据帧,然后将每个答案添加到列中

[英]Multiply each value in two data frames and add each answer to a column

I am trying to multiply each value in two data frames(df2 and df3) and add each answer to a column in one of the data frames. 我试图将每个值乘以两个数据帧(df2和df3),并将每个答案添加到其中一个数据帧的列中。 df2 consists of data where the columns are the types and the rows are how much they cost on average each day. df2由数据组成,其中列是类型,行是它们平均每天要花费多少。 df3 consists of how many of each car is purchased. df3由每辆汽车购买多少组成。

I want to multiply each column and row for each day and add it to a column already created in df3 called Money. 我想每天增加每一列和每一行,并将其添加到已在df3中创建的称为Money的列中。

I already have a for loop set up and df1 is where I can tell if someone purchased or sold the car and am hoping to somehow incorporate a line in there to loop through each row of df2 and multiply it by df3 and add that amount for each day in the column Money: 我已经设置了for循环,而df1可以告诉我是否有人购买或出售了汽车,并希望以某种方式在其中并入一行以循环遍历df2的每一行,并将其乘以df3,然后为每行相加货币栏中的一天:

for i, rows in df1.iterrows():
    if rows.Buy == 'purchase':
        df3.ix[rows.Day,rows.Type] = row.AmountOfCars
        df3.ix[row.day, 'Money '] -= df3.ix[row.Day,rows.type] * df2???

df1(amount)         Audi    BMW     Buy        
2010-03-16          2000    4000    purchase
2010-04-19          2500    4500    sold

df2(prices)      Audi      BMW   
    2010-03-16  450000    350000    
    2010-04-19  6500000   350300  

df3(totals)      Audi      BMW  Money 
    2010-03-16  2000      4000    ?
    2010-04-19  -2500    -4500    ?

I think you can compare string by str.contains , use loc and sum : 我认为您可以通过str.contains比较字符串,使用locsum

print df1['Buy'].str.contains('purchase')
2010-03-16     True
2010-04-19    False
Name: Buy, dtype: bool

print (df3 * df2).sum(axis=1)
2010-03-16     2300000000
2010-04-19   -17826350000
dtype: int64

df3.loc[ df1['Buy'].str.contains('purchase'), 'Money'] = (df3 * df2).sum(axis=1)
print df3
            Audi   BMW       Money
2010-03-16  2000  4000  2300000000
2010-04-19 -2500 -4500         NaN

If you want only some types of cars use list cars : 如果您只想要某些类型的汽车,请使用list cars

cars = ['Audi', 'BMW']
print (df3[cars] * df2[cars]).sum(axis=1)
2010-03-16     2300000000
2010-04-19   -17826350000
dtype: int64

df3.loc[ df1['Buy'].str.contains('purchase'), 'Money'] = (df3[cars] * df2[cars]).sum(axis=1)
print df3
            Audi   BMW       Money
2010-03-16  2000  4000  2300000000
2010-04-19 -2500 -4500         NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM