【发布时间】:2020-06-14 07:08:58
【问题描述】:
我有一个样本 DF,我想根据 2 个条件对其进行标准化
创建样本 DF:
sample_df = pd.DataFrame(np.random.randint(1,20,size=(10, 3)), columns=list('ABC'))
sample_df["date"]= ["2020-02-01","2020-02-01","2020-02-01","2020-02-01","2020-02-01",
"2020-02-02","2020-02-02","2020-02-02","2020-02-02","2020-02-02"]
sample_df["date"] = pd.to_datetime(sample_df["date"])
sample_df.set_index(sample_df["date"],inplace=True)
del sample_df["date"]
sample_df["A_cat"] = ["ind","sa","sa","sa","ind","ind","sa","sa","ind","sa"]
sample_df["B_cat"] = ["sa","ind","ind","sa","sa","sa","ind","sa","ind","sa"]
sample_df
print (sample_df)
操作:
A B C A_cat B_cat
date
2020-02-01 14 11 7 ind sa
2020-02-01 19 17 3 sa ind
2020-02-01 19 6 3 sa ind
2020-02-01 3 16 5 sa sa
2020-02-01 12 6 16 ind sa
2020-02-02 1 8 12 ind sa
2020-02-02 10 13 19 sa ind
2020-02-02 17 2 7 sa sa
2020-02-02 9 13 17 ind ind
2020-02-02 17 16 3 sa sa
标准化条件:
1. Groupby based on index, and
2. Nomalize selected columns
例如,如果选择的列是["A","B"],则在这种情况下应该首先对2020-02-01 进行分组索引,并在该组的5 行中规范化选择的列。
其他输入:
selected_column = ["A","B"]
我可以在for loop 中通过迭代组并连接标准化值来做到这一点。因此,任何关于更有效/基于熊猫的方法的建议都会很棒。
用 Pandas 尝试过的代码:
from sklearn.preprocessing import StandardScaler
dfg = StandardScaler()
sample_df.groupby([sample_df.index.get_level_values(0)])[selected_columns].transform(dfg.fit_transform)
错误:
('Expected 2D array, got 1D array instead:\narray=[14. 19. 19. 3. 12.].\nReshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.', 'occurred at index A')
【问题讨论】:
标签: python pandas scikit-learn pandas-groupby sklearn-pandas