[英]create a new dataframes with rows from another dataframe with specific columns values
[英]how to create columns from another dataframe rows and populate them based on values from both dataframes
通過new_col,attribute
和多個不帶id
的df2
中的所有列創建MultiIndex Series
,通過DataFrame.droplevel
刪除 MultiIndex 的第二級,加入df2
並最后使用DataFrame.convert_dtypes
作為整數列:
s = df1.set_index(['new_col','attribute'])['y1']
df = df2.join(df2.drop('id',1).mul(s, level=1).droplevel(1, axis=1)).convert_dtypes()
print (df)
id car hgv petrol diesel rigid artic
0 1 10 1000 3 4 250.0 350.0
1 2 1000 10 300 400 2.5 3.5
2 3 100 10000 30 40 2500.0 3500.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.