简体   繁体   English

如何从另一个 Pandas dataframe 逐列缩放

[英]how to scale columns by column from another Pandas dataframe

I have a Pandas data frame 'df' in which I'd like to perform some scalings column by column.我有一个 Pandas 数据框“df”,我想在其中逐列执行一些缩放。

In column 'A', I need to scale it from the row with id 'A'.在“A”列中,我需要从 ID 为“A”的行对其进行缩放。 In column 'B', I need to scale it from the row with id 'B'.在“B”列中,我需要从 id 为“B”的行对其进行缩放。 ... ...

l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]

df = pd.DataFrame([z for z in zip(l1,l2,l3)], columns= ['A', 'B', 'C'])

scaling = pd.DataFrame(dict(id=['A', 'B','C'], scaling = [0.2, 0.3, 0.4]))

What would be the best way to do it?最好的方法是什么?

How about:怎么样:

for col in df.columns:
    df[col] = df[col] * scaling.loc[scaling['id'] == col]["scaling"].item()

Assuming the columns are unique, and there are no duplicates in scaling , you could use map :假设列是唯一的,并且在scaling中没有重复,您可以使用map

df.mul(df.columns.map(scaling.set_index("id").scaling))

     A    B    C
0  0.2  1.2  2.8
1  0.4  1.5  3.2
2  0.6  1.8  3.6

暂无
暂无

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

相关问题 基于另一列的 Pandas 数据框比例列 - Pandas dataframe scale column based on another column 如何使用 Numpy 通过来自另一个 DataFrame 的因子来缩放 DataFrame 中的列 - how to scale columns in a DataFrame by factors from another DataFrame using Numpy 如果另一个列中的值是另一个DataFrame中的pandas列? - pandas columns from another DataFrame if value is in another column? 如何获得大熊猫数据框,其中列是来自另一列数据框的后续n元素? - How to get pandas dataframe where columns are the subsequent n-elements from another column dataframe? 如何从 pandas dataframe 列返回最大十进制“精度”和“比例”? - How to return the maximum decimal "precision" and "scale" from pandas dataframe column? 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame Pandas - 如何使用另一个 dataframe 从 dataframe 中提取列? - Pandas - How to extract columns from a dataframe using another dataframe? 如何从数据框中的其他列创建新的Pandas数据框列 - How to create a new Pandas dataframe column from other columns in the dataframe 如何根据在另一个 dataframe 中定义的行/列缩放因子来缩放 pandas dataframe? - How can I scale a pandas dataframe based on row/column scaling factors defined in another dataframe? 如何将 pandas dataframe 中的列复制到另一个列,同时匹配两者中公共列的值? - How to copy a column from a pandas dataframe to another while matching values of common columns in both?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM