2
我使用Python 2.7.11和Anaconda。爲熊貓數據框中的行子集隨機賦值
我知道如何設置Pandas
DataFrame
的行子集的值,如Modifying a subset of rows in a pandas dataframe,但我需要隨機設置這些值。
說我有下面的數據幀df
。我如何隨機設置group == 2
的值,使它們不都等於1.0?
import pandas as pd
import numpy as np
df = pd.DataFrame([1,1,1,2,2,2], columns = ['group'])
df['value'] = np.nan
df.loc[df['group'] == 2, 'value'] = np.random.randint(0,5)
print df
group value
0 1 NaN
1 1 NaN
2 1 NaN
3 2 1.0
4 2 1.0
5 2 1.0
df
應該看起來像下面:
print df
group value
0 1 NaN
1 1 NaN
2 1 NaN
3 2 1.0
4 2 4.0
5 2 2.0