简体   繁体   English

熊猫将多列相乘以创建新的df

[英]pandas multiply multiple columns to make new df

I'm starting with two dataframes - one filled with dummy variables for the day of the week, and another filled with dummies for the hour of the day (HE - hour ending). 我从两个数据帧开始-一个在一周中的一天填充虚拟变量,另一个在一天中的某个小时填充虚拟变量(HE-小时结束)。

df1 = days of week = nxm, where n is number of observations, m is 6: df1 =星期几= nxm,其中n是观测次数,m是6:

    Mon Tue Wed Thu Fri Sat
0   1   0   0   0   0   0
1   0   1   0   0   0   0
2   0   0   1   0   0   0
3   0   0   0   1   0   0

df2 = hours of the day = nxl, where n is num of obs, m is 23: df2 =一天中的小时数= nxl,其中n是整数,m是23:

    HE1 HE2 HE3 HE4 HE5 ... HE22 HE23
0   1   0   0   0   0   ... 0    0
1   0   1   0   0   0   ... 0    0
2   0   0   1   0   0   ... 0    0
3   0   0   0   1   0   ... 0    0

I want to create a new df with dimensions nx (m times l), where m times l = 6*23 = 138: 我想创建一个尺寸为nx(m×l)的新df,其中m×l = 6 * 23 = 138:

    MonHE1 MonHE2 MonHE3 MonHE4 MonHE5 ... SatHE22 SatHE23
0   1      0      0      0      0      ... 0       0
1   0      1      0      0      0      ... 0       0
2   0      0      1      0      0      ... 0       0
3   0      0      0      1      0      ... 0       0

I am successful with this: 我成功了:

hoursXdays = pd.DataFrame()
Mon = hours.multiply(days['Mon'],axis='index').add_prefix('Mon')
Tue = hours.multiply(days['Tue'],axis='index').add_prefix('Tue')
Wed = hours.multiply(days['Wed'],axis='index').add_prefix('Wed')
Thu = hours.multiply(days['Thu'],axis='index').add_prefix('Thu')
Fri = hours.multiply(days['Fri'],axis='index').add_prefix('Fri')
Sat = hours.multiply(days['Sat'],axis='index').add_prefix('Sat')
hoursXdays = pd.concat([Mon, Tue, Wed, Thu, Fri, Sat],axis=1)

I'm going to need to do this for a lot more dfs than just these few. 我将需要这样做,而不仅仅是少数几个。 If there's any way I can generalize this, I'd appreciate the help. 如果有什么可以概括的方法,我将不胜感激。

Basically, is there a way in pandas to multiply two dataframes dimension nxm, nxl, to make a new df of dimension nx (m times l) ? 基本上,在熊猫中,是否有一种方法可以将两个维度为nxm,nxl的数据帧相乘,以生成维度为nx(m乘以l)的新df?

你可以在这里使用for循环

pd.concat([hours.multiply(days[x],axis='index').add_prefix(x) for x in days.columns],1)

You can use itertools.product and pd.DataFrame default constructor 您可以使用itertools.productpd.DataFrame默认构造函数

d = {a[0]+b[0]: np.array(a[1])*b[1] for a,b in itertools.product(df.to_dict('list').items(), df2.to_dict('list').items())}

dff = pd.DataFrame(d)

MonHE1  MonHE2  ... SatHE22 SatHE23
0   1   0       ... 0       0
1   0   0       ... 0       0
2   0   0       ... 0       0
3   0   0       ... 0       0

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

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