我有一个pandas数据帧,我希望用随机正常数替换一些唯一值。在下面的示例中,要替换的值为0。
import numpy as np
import pandas as pd
dates = pd.date_range('20160101', periods=10)
x = [1.0,2.0,10.0,9.0,0,7.0,6.0,0,3.0,9.0]
df = pd.DataFrame(x,index=dates,columns=['A'])
A
2016-01-01 1.000000
2016-01-02 2.000000
2016-01-03 10.000000
2016-01-04 9.000000
2016-01-05 0.000000
2016-01-06 7.000000
2016-01-07 6.000000
2016-01-08 0.000000
2016-01-09 3.000000
2016-01-10 9.000000
这就是我所拥有的:
df['A'] = df.A.replace(to_replace =0, value = np.random.normal(0,1))
用相同的值替换零。
A
2016-01-01 1.000000
2016-01-02 2.000000
2016-01-03 10.000000
2016-01-04 9.000000
2016-01-05 6.993988
2016-01-06 7.000000
2016-01-07 6.000000
2016-01-08 6.993988
2016-01-09 3.000000
2016-01-10 9.000000
我想要不同的价值观。我怎么能这样做?
答案 0 :(得分:3)
试试这个:
In [51]:
dates = pd.date_range('20160101', periods=10)
x = [1.0,2.0,10.0,9.0,0,7.0,6.0,0,3.0,9.0]
df = pd.DataFrame(x,index=dates,columns=['A'])
df
Out[51]:
A
2016-01-01 1
2016-01-02 2
2016-01-03 10
2016-01-04 9
2016-01-05 0
2016-01-06 7
2016-01-07 6
2016-01-08 0
2016-01-09 3
2016-01-10 9
In [56]:
df.loc[df['A'] == 0,'A'] = np.random.normal(0,1, len(df.loc[df['A'] == 0]))
df
Out[56]:
A
2016-01-01 1.000000
2016-01-02 2.000000
2016-01-03 10.000000
2016-01-04 9.000000
2016-01-05 0.259048
2016-01-06 7.000000
2016-01-07 6.000000
2016-01-08 0.623833
2016-01-09 3.000000
2016-01-10 9.000000
基本上你需要传递随机样本的数量来生成,因为你没有传递一个大小,它会返回一个标量值,所以所有被替换的值都是相同的。
请参阅文档:http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.random.normal.html
也可以在这里使用apply
:
In [94]:
df.loc[df['A'] == 0,'A'] = df['A'].apply(lambda x: np.random.normal(0,1))
df
Out[94]:
A
2016-01-01 1.000000
2016-01-02 2.000000
2016-01-03 10.000000
2016-01-04 9.000000
2016-01-05 2.794664
2016-01-06 7.000000
2016-01-07 6.000000
2016-01-08 -0.524947
2016-01-09 3.000000
2016-01-10 9.000000
答案 1 :(得分:2)
我最近遇到了类似的问题并创建了一个函数。试试这个修改过的功能:
def replace_zeros_w_random_normal(DF,label, mu, sigma):
truth_1 = DF[label] == 0
random = np.random.normal(mu, sigma, DF.shape[0])
filt = DF[DF[label] > 0]
vector_1 = truth_1 * random
truth_2 = vector_1 == 0
vector_2 = truth_2 * DF[label]
DF[label] = np.maximum(vector_1,vector_2)
return DF
然后运行:
replace_zeros_w_random_normal(df,'A ,1,0.1)