我想創建一個 DataFrame,其中包括 10 小時內 n 個消費者(這里 n = 5)的每小時熱量需求。--> DataFrame 稱為“Village”,n 列(每列代表一個消費者)和 10 行(10 小時)所有消費者都遵循相同的需求組態檔,唯一的區別是它在隨機的小時內轉移。亂數服從正態分布。
我設法創建了一個遵循正態分布的離散數字串列,并且我設法創建了一個包含 n 行的 DataFrame,其中相同的需求組態檔被該亂數移位。
我無法解決的問題是,出現 NaN 而不是用由于班次而被削減的值填充班次。
示例:如果需求組態檔移動了 1 小時(例如消費者 5)。現在出現“NaN”作為第一個小時的需求。而不是“NaN”,我希望出現原始需求組態檔的第 10 小時的值(4755.005240)。因此,與其改變需求概況的價值,我更希望它“輪換”。
heat_demand
0 1896.107462
1 1964.878199
2 2072.946499
3 2397.151402
4 3340.292937
5 4912.195496
6 6159.893152
7 5649.024821
8 5157.805271
9 4755.005240
Consumer 1 Consumer 2 Consumer 3 Consumer 4 Consumer 5
0 1896.107462 NaN 1964.878199 NaN NaN
1 1964.878199 NaN 2072.946499 NaN 1896.107462
2 2072.946499 NaN 2397.151402 NaN 1964.878199
3 2397.151402 1896.107462 3340.292937 1896.107462 2072.946499
4 3340.292937 1964.878199 4912.195496 1964.878199 2397.151402
5 4912.195496 2072.946499 6159.893152 2072.946499 3340.292937
6 6159.893152 2397.151402 5649.024821 2397.151402 4912.195496
7 5649.024821 3340.292937 5157.805271 3340.292937 6159.893152
8 5157.805271 4912.195496 4755.005240 4912.195496 5649.024821
9 4755.005240 6159.893152 NaN 6159.893152 5157.805271
有人可以給我一個提示如何解決這個問題嗎?非常感謝提前和親切的問候
路易絲
import numpy as np
import pandas as pd
import os
path= os.path.dirname(os.path.abspath(os.path.join(file)))
#Create a list with discrete numbers following normal distribution
n = 5
timeshift_1h = np.random.normal(loc=0.1085, scale=1.43825, size=n)
timeshift_1h = np.round(timeshift_1h).astype(int)
print ("Time Shift in h:", timeshift_1h)
#Read the Standard Load Profile
cols = ["heat_demand"]
df_StandardLoadProfile = pd.read_excel(os.path.join(path, '10_h_example.xlsx'),usecols=cols)
print(df_StandardLoadProfile)
#Create a df for n consumers, whose demand equals a shifted StandardLoadProfile.
#It is shifted by a random amount of hours, that is taken from the list timeshift_1h
list_consumers = list(range(1,n 1))
Village=pd.DataFrame()
for i in list_consumers:
a=timeshift_1h[i-1]
name = "Consumer {}".format(i)
Village[name] = df_StandardLoadProfile.shift(a)
print(Village)
uj5u.com熱心網友回復:
該用例有一個非常好的 numpy 函式,即np.roll(有關檔案,請參見此處)。它接受一個陣列并按指定的步驟移動它shift。
對于您的示例,這可能如下所示:
import pandas as pd
import numpy as np
df = pd.read_csv("demand.csv")
df['Consumer 1'] = np.roll(df["heat_demand"], shift=1)
uj5u.com熱心網友回復:
您可以填充nan反轉列中的值 -
df = pd.DataFrame(np.arange(10))
df
# 0
#0 0
#1 1
#2 2
#3 3
#4 4
#5 5
#6 6
#7 7
#8 8
#9 9
df[0].shift(3).fillna(pd.Series(reversed(df[0])))
#0 9.0
#1 8.0
#2 7.0
#3 0.0
#4 1.0
#5 2.0
#6 3.0
#7 4.0
#8 5.0
#9 6.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455786.html
