Python 基于不同Dataframe中的值创建新Dataframe
我试图根据dataframe的前一列中的值和另一个dataframe中的值创建附加dataframe 我正在处理的问题要求我在100个时间段内为50个人生成一个从0到1的随机数。每个人从位置x=0开始,并有可能在x=0和x=1之间来回移动。如果他们在该期间生成的数字大于0.9,他们将切换位置(因此,如果他们在上一期间处于x=0,他们将移动到x=1;如果他们在上一期间处于x=1,他们将移动到x=0)。如果该数字不大于0.9,则它们将与上一时间段保持在同一位置。我试图找到每个人在每个时间段的位置 以下是我到目前为止的情况:Python 基于不同Dataframe中的值创建新Dataframe,python,pandas,loops,numpy,dataframe,Python,Pandas,Loops,Numpy,Dataframe,我试图根据dataframe的前一列中的值和另一个dataframe中的值创建附加dataframe 我正在处理的问题要求我在100个时间段内为50个人生成一个从0到1的随机数。每个人从位置x=0开始,并有可能在x=0和x=1之间来回移动。如果他们在该期间生成的数字大于0.9,他们将切换位置(因此,如果他们在上一期间处于x=0,他们将移动到x=1;如果他们在上一期间处于x=1,他们将移动到x=0)。如果该数字不大于0.9,则它们将与上一时间段保持在同一位置。我试图找到每个人在每个时间段的位置 以
#First I generated a 50x100 dataframe of random numbers
rv = pd.DataFrame(np.random.uniform(low = 0.0, high = 1.0, size=(51, 100)), columns = range(1,101))
#print(rv)
#Next, I created a new dataframe that tells me whether the value in the random number dataframe is greater than 0.9
rv_2 = rv[rv > 0.9].notnull()
#rv_2
#this is where I am stuck right now
#First I made a dataframe where every individual starts at location x=0
rv_3 = pd.DataFrame(0, index = range(51), columns = range(1))
#Here I am trying to append the location of each individual for each time period
#I was trying to add a 1 in the rv_3 dataframe if the previous column had a 0 and the corresponding value in the rv_2 dataframe was true, and so on
for col in rv_2:
for i, row_value in rv_2[col].iteritems():
if rv_2[col][i] == True & rv_3[col][i-1] == 0:
rv_3[col][i] = 1
elif rv_2[col][i] == True & rv_3[col][i-1] == 1:
rv_3[col][i] = 1
else:
rv_3[col][i] = rv_3[col][i-1]
提前谢谢你 这对你有用吗?rv_2.cumsum(axis='columns')[100]%2我不这么认为。我需要能够绘制每个时间段中每个个体的位置,而不仅仅是结束时间段。我选择了最后一步,如果您需要在每个步骤中使用
rv_2.cumsum(axis='columns')%2