df + np.random.rand(*df.shape)/10000.0
或
讓我們使用applymap
:
df = pd.DataFrame(1.0, index=[1,2,3,4,5], columns=list('ABC'))
df.applymap(lambda x: x + np.random.rand()/10000.0)
輸出:
A \
1 [[1.00006953418, 1.00009164785, 1.00003177706]...
2 [[1.00007291245, 1.00004186046, 1.00006935173]...
3 [[1.00000490127, 1.0000633115, 1.00004117181],...
4 [[1.00007159622, 1.0000559506, 1.00007038891],...
5 [[1.00000980335, 1.00004760836, 1.00004214422]...
B \
1 [[1.00000320322, 1.00006981682, 1.00008912557]...
2 [[1.00007443802, 1.00009270815, 1.00007225764]...
3 [[1.00001371778, 1.00001512412, 1.00007986851]...
4 [[1.00005883343, 1.00007936509, 1.00009523334]...
5 [[1.00009329606, 1.00003174878, 1.00006187704]...
C
1 [[1.00005894836, 1.00006592776, 1.0000171843],...
2 [[1.00009085391, 1.00006606979, 1.00001755092]...
3 [[1.00009736701, 1.00007240762, 1.00004558753]...
4 [[1.00003981393, 1.00007505714, 1.00007209959]...
5 [[1.0000031608, 1.00009372917, 1.00001960112],...
爲什麼不只是'df = pd.DataFrame(np.random.randn(3,5),columns = list('ABC'))'這裏'-0.5/1000.0'的意義是什麼 – EdChum
這樣' df.apply(lambda x:x +(np.random.rand() - 0.5)/ 1000)'工作正常 –
e.arbitrio沒有工作。我得到了每列中所有行的相同隨機數。 – TPM