简体   繁体   English

将一系列值随机插入 pd.dataframe

[英]Insert a series of values into pd.dataframe randomly

I have a large dataframe and what I want to do is overwrite X entries of that dataframe with a new value I set.我有一个大数据框,我想要做的是用我设置的新值覆盖该数据框的 X 个条目。 The new entries have to be at a random position, but they have to be in order.新条目必须位于随机位置,但必须按顺序排列。 Like I have a Column with random numbers, and want to overwrite 20 of them in a row with the new value x.就像我有一个带有随机数的列,并且想用新值 x 连续覆盖其中的 20 个。

I tried df.sample(x) and then update the dataframe, but I only get individual entries.我试过df.sample(x)然后更新数据df.sample(x) ,但我只得到单个条目。 But I need the X new entries in a row (consecutively).但是我需要连续的 X 个新条目(连续)。

Somebody got a solution?有人有解决方案吗? I'm quite new to Python and have to get into it for my master thesis.我对 Python 很陌生,必须在我的硕士论文中学习它。

CLARIFICATION:澄清:

My dataframe has 5 columns with almost 60,000 rows, each row for 10 minutes of the year.我的数据框有 5 列,几乎有 60,000 行,每行一年中的 10 分钟。

  • One Column is 'output' with electricity production values for that 10 minutes.一列是“输出”,其中包含 10 分钟的发电量值。
  • For 2 consecutive hours (120 consecutive minutes, hence 12 consecutive rows) of the year I want to lower that production to 60%.在一年中连续 2 小时(连续 120 分钟,因此连续 12 行),我想将该产量降低到 60%。 I want it to happen at a random time of the year.我希望它发生在一年中的随机时间。
  • Another column is 'status', with information about if the production is reduced or not.另一列是“状态”,包含有关产量是否减少的信息。

I tried:我试过:

df_update = df.sample(12)
df_update.status = 'reduced'
df.update(df_update)
df.loc[('status) == 'reduced', ['production']] *=0.6 

which does the trick for the total amount of time (12*10 minutes), but I want 120 consecutive minutes and not separated.这对总时间(12 * 10 分钟)有效,但我想要连续 120 分钟而不是分开。

I decided to get a random value and just index the next 12 entries to be 0.6.我决定获取一个随机值并将接下来的 12 个条目索引为 0.6。 I think this is what you want.我想这就是你想要的。

df = pd.DataFrame({'output':np.random.randn(20),'status':[0]*20})
idx = df.sample(1).index.values[0]
df.loc[idx:idx+11,"output"]=0.6
df.loc[idx:idx+11,"status"]=1

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

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