我将优化代码
我试图通过仅更改“值”列的值并保留其他列的值来从DataFrame的先前值创建新行。
我不知道这样做是否很好,因为“ tmp_df[:1]['Value']
”会导致很多数据花费很长时间。
我只是发现这种方式。如果您有最佳化的想法:
import random
import pandas as pd
import numpy as np
d = {'WorkerId': [1, 2] ,'Value': [3, 4,],'WeekDay':['a','b']}
df = pd.DataFrame(data=d)
Listweek=['a','b'] #my week list
WorkerIdList=[1,2] # my worker ID
#creating a DataFrame with the same column name as df
df2 = pd.DataFrame(columns=df.columns)
for workerid in WorkerIdList:
for week in Listweek:
if(not df[ (df.WeekDay==week)].empty):
# I am taking the first row because I want to keep some value
tmp_df=df[ (df.WeekDay==week)][:1]
#Then I change a the value on the column "Value"
tmp_df[:1]['Value']= df[(df['WorkerId']==workerid)]['Value'].iloc[0] + random.randint(1, 10)
#I am doing concatenation
frames = [df2, tmp_df[:1]]
df2 = pd.concat(frames)
df2
答案 0 :(得分:0)
因为不清楚您想要什么,我唯一能给您的就是
df2=df.copy()
df2.Value=df2.Value+random.randint(1,10)
在没有声明的情况下也会给出类似的结果
Listweek=['a','b'] #my week list
WorkerIdList=[1,2] # my worker ID