![](/img/trans.png)
[英]Is there a way to output df.min, df.max and df.mean in Pandas.groupby on a certain column at once?
[英]How to apply df.min() and df.max() distributively over many DataFrames
我有一個DataFrames列表,所有這些列都具有相同的列,並且想將所有它們中的值標准化為[0,1],同時還要保持DataFrames之間值的相對縮放。
對於單個DataFrame,我可以這樣做:
df = (df - df.min()) / (df.max() - df.min())
但是,如何獲取所有DataFrame中每一列的最小值和最大值,然后使用每一列的集合最小值和最大值將與上述相同的公式應用於每個單獨的DataFrame?
這是2個DataFrame的示例列表,每個DataFrame均在其上進行了規范化:
import numpy as np
import pandas as pd
dfs = []
for i in range(2):
data = np.random.rand(3, 3)
df = pd.DataFrame(data, columns=["one", "two", "three"])
dfs.append(df)
print(df)
for i in range(2):
dfs[i] = (dfs[i] - dfs[i].min()) / (dfs[i].max() - dfs[i].min())
print(dfs[i])
我會concat
在DFS一個df和使用計算的多指標
df=pd.concat(dfs,keys=range(len(dfs)))
df =df.groupby(level=0).apply(lambda x : (x - x.min()) / (x.max() - x.min()))
如果您想看第一張df
df.loc[0]
Out[20]:
one two three
0 1.000000 0.576142 0.000000
1 0.559371 0.000000 1.000000
2 0.000000 1.000000 0.594986
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.