简体   繁体   English

按日期从 dataframe 列中随机选择行

[英]Randomly selecting rows from dataframe column by date

For a given dataframe column, I would like to randomly select by day roughly 60% and add to a new column, add the remaining 40% to another column, multiply the 40% column by (-1), and create a new column that merges these back together for each day (so that each day I have a ratio of 60/40) :对于给定的 dataframe 列,我想按天随机 select 大约 60% 并添加到新列中,将剩余的 40% 添加到另一列中,将 40% 列乘以 (-1),然后创建一个新列每天将这些重新合并在一起(这样每天我的比例为 60/40)

I have asked the same question without the daily specification here: Randomly selecting rows from dataframe column我在这里问了同样的问题,但没有每日规范: Randomly selection rows from dataframe column

Example below illustrates this (although my ratio is not exactly 60/40 there):下面的示例说明了这一点(尽管我的比率不完全是 60/40):

dict0 = {'date':[1/1/2019,1/1/2019,1/1/2019,1/2/2019,1/1/2019,1/2/2019],'x1': [1,2,3,4,5,6]}
df = pd.DataFrame(dict0)### 
df['date']      = pd.to_datetime(df['date']).dt.date 

dict1 = {'date':[1/1/2019,1/1/2019,1/1/2019,1/2/2019,1/1/2019,1/2/2019],'x1': [1,2,3,4,5,6],'x2': [1,'nan',3,'nan',5,6],'x3': ['nan',2,'nan',4,'nan','nan']}
df = pd.DataFrame(dict1)### 
df['date']      = pd.to_datetime(df['date']).dt.date 

dict2 = {'date':[1/1/2019,1/1/2019,1/1/2019,1/2/2019,1/1/2019,1/2/2019],'x1': [1,2,3,4,5,6],'x2': [1,'nan',3,'nan',5,6],'x3': ['nan',-2,'nan',-4,'nan','nan']}
df = pd.DataFrame(dict2)### 
df['date']      = pd.to_datetime(df['date']).dt.date 

dict3 = {'date':[1/1/2019,1/1/2019,1/1/2019,1/2/2019,1/1/2019,1/2/2019],'x1': [1,2,3,4,5,6],'x2': [1,'nan',3,'nan',5,6],'x3': ['nan',-2,'nan',-   4,'nan','nan'],'x4': [1,-2,3,-4,5,6]}
df = pd.DataFrame(dict3)### 
df['date']      = pd.to_datetime(df['date']).dt.date 

you can use groupby and sample , get the index values, then create the column x4 with loc, and fillna with the -1 multiplied column like:您可以使用groupbysample ,获取index值,然后使用 loc 创建列 x4,并使用 -1 相乘的列创建fillna ,例如:

idx= df.groupby('date').apply(lambda x: x.sample(frac=0.6)).index.get_level_values(1)
df.loc[idx, 'x4'] = df.loc[idx, 'x1']
df['x4'] = df['x4'].fillna(-df['x1'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM