如何根据列值将规范化一分为二?
How can I split my normalization in two according to column values?
嗨,我在 pandas 中有一个列数据,其分布非常倾斜:
我按照截断值 1000 将数据一分为二,这是两组的分布。
现在,我想用 0-1 之间的值进行归一化。我想执行 'differential' 归一化,左面板的值在 0-0.5 之间归一化,右面板的值在 0.5 到 1 之间归一化,所有内容都在同一列中。我该怎么做?
假设您的数据框名为 df
,保存数据的列称为 data
,保存计数的列称为 counts
。然后你可以这样做:
df['data_norm'] = df['data'].loc[df['counts']<=1000] / 1000 / 2
df['data_norm'] = df['data'].loc[df['counts']>1000] / df['counts'].max() + 0.5
...假设我理解正确。但我想我既没有正确理解你的问题,也没有正确理解你的解决方法。
它不漂亮,但很管用。
df = pd.DataFrame({'dataExample': [0,1,2,1001,1002,1003]})
less1000 = df.loc[df['dataExample'] <= 1000]
df.loc[df['dataExample'] <= 1000, 'datanorm'] = less1000['dataExample'] / (less1000['dataExample'].max() * 2)
high1000 = df.loc[df['dataExample'] > 1000]
df.loc[df['dataExample'] > 1000, 'datanorm'] = ((high1000['dataExample'] - high1000['dataExample'].min()) / ((high1000['dataExample'].max() - high1000['dataExample'].min()) * 2) + 0.5)
output:
dataExample datanorm
0 0 0.00
1 1 0.25
2 2 0.50
3 1001 0.50
4 1002 0.75
5 1003 1.00
嗨,我在 pandas 中有一个列数据,其分布非常倾斜:
我按照截断值 1000 将数据一分为二,这是两组的分布。
现在,我想用 0-1 之间的值进行归一化。我想执行 'differential' 归一化,左面板的值在 0-0.5 之间归一化,右面板的值在 0.5 到 1 之间归一化,所有内容都在同一列中。我该怎么做?
假设您的数据框名为 df
,保存数据的列称为 data
,保存计数的列称为 counts
。然后你可以这样做:
df['data_norm'] = df['data'].loc[df['counts']<=1000] / 1000 / 2
df['data_norm'] = df['data'].loc[df['counts']>1000] / df['counts'].max() + 0.5
...假设我理解正确。但我想我既没有正确理解你的问题,也没有正确理解你的解决方法。
它不漂亮,但很管用。
df = pd.DataFrame({'dataExample': [0,1,2,1001,1002,1003]})
less1000 = df.loc[df['dataExample'] <= 1000]
df.loc[df['dataExample'] <= 1000, 'datanorm'] = less1000['dataExample'] / (less1000['dataExample'].max() * 2)
high1000 = df.loc[df['dataExample'] > 1000]
df.loc[df['dataExample'] > 1000, 'datanorm'] = ((high1000['dataExample'] - high1000['dataExample'].min()) / ((high1000['dataExample'].max() - high1000['dataExample'].min()) * 2) + 0.5)
output:
dataExample datanorm
0 0 0.00
1 1 0.25
2 2 0.50
3 1001 0.50
4 1002 0.75
5 1003 1.00