我正在使用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
答案 0 :(得分:4)
您必须确定第2组的大小
g2 = df['group'] == 2
df.loc[g2, 'value'] = np.random.randint(5, size=g2.sum())
print(df)
group value
0 1 NaN
1 1 NaN
2 1 NaN
3 2 3.0
4 2 4.0
5 2 2.0