Create dataframe based on random floats
Question:
I need to create a dataframe based on whether an input is greater or smaller than a randomly generated float.
At current, I’m not sure how you can refer to a previous column in pandas and then use a function on this to append the column.
The following photo essentially explains what im trying to do in python, where the 1 & 0 are the states, and depending on this, a 1 or 0 are added to the column.
the result of the excel is as follows:
As you can see, when the above cell is 0, the forumla if C10 < pdw then C9 will be 1 otherwise will be 0.
i know this explanation is not very good, so if anything needs to be explained more clearly let me know and ill try my best to update the post with more information.
here is the very minimal code I have for more context:
import numpy as np
import pandas as pd
import random as rd
#pww = float(input("pww value: n"))
#pdd = float(input("pdd value: n"))
pww = 0.7
pdd = 0.3
pwd = float(1 - pww)
pdw = float(1 - pdd)
rainfall = pd.DataFrame()
random = {
"Random 1": np.random.rand(3650),
"Random 2": np.random.rand(3650)
}
randomdf = pd.DataFrame(random)
state = [1]
Answers:
From what I understand you want to create a DataFrame with two random number columns and a state column which will be populated based on the described logic.
The states will be calculated based on the previous state and the value in the "Random 2" column. It will then add the calculated states as a new column to the DataFrame.
Following is the continuation of the implementation:
import numpy as np
import pandas as pd
pww = 0.7
pdd = 0.3
pwd = 1 - pww
pdw = 1 - pdd
random_numbers = pd.DataFrame({
"Random 1": np.random.rand(3650),
"Random 2": np.random.rand(3650)
})
# Initialize the states list with the first state
states = [1]
# Iterate over the random numbers
for i in range(1, len(random_numbers)):
prev_state = states[-1]
random_value = random_numbers.loc[i, "Random 2"]
if prev_state > 0:
new_state = 1 if random_value < pww else 0
else:
new_state = 1 if random_value < pdw else 0
states.append(new_state)
# Add states column to the DataFrame
random_numbers["State"] = states
print(random_numbers)
I need to create a dataframe based on whether an input is greater or smaller than a randomly generated float.
At current, I’m not sure how you can refer to a previous column in pandas and then use a function on this to append the column.
The following photo essentially explains what im trying to do in python, where the 1 & 0 are the states, and depending on this, a 1 or 0 are added to the column.
the result of the excel is as follows:
As you can see, when the above cell is 0, the forumla if C10 < pdw then C9 will be 1 otherwise will be 0.
i know this explanation is not very good, so if anything needs to be explained more clearly let me know and ill try my best to update the post with more information.
here is the very minimal code I have for more context:
import numpy as np
import pandas as pd
import random as rd
#pww = float(input("pww value: n"))
#pdd = float(input("pdd value: n"))
pww = 0.7
pdd = 0.3
pwd = float(1 - pww)
pdw = float(1 - pdd)
rainfall = pd.DataFrame()
random = {
"Random 1": np.random.rand(3650),
"Random 2": np.random.rand(3650)
}
randomdf = pd.DataFrame(random)
state = [1]
From what I understand you want to create a DataFrame with two random number columns and a state column which will be populated based on the described logic.
The states will be calculated based on the previous state and the value in the "Random 2" column. It will then add the calculated states as a new column to the DataFrame.
Following is the continuation of the implementation:
import numpy as np
import pandas as pd
pww = 0.7
pdd = 0.3
pwd = 1 - pww
pdw = 1 - pdd
random_numbers = pd.DataFrame({
"Random 1": np.random.rand(3650),
"Random 2": np.random.rand(3650)
})
# Initialize the states list with the first state
states = [1]
# Iterate over the random numbers
for i in range(1, len(random_numbers)):
prev_state = states[-1]
random_value = random_numbers.loc[i, "Random 2"]
if prev_state > 0:
new_state = 1 if random_value < pww else 0
else:
new_state = 1 if random_value < pdw else 0
states.append(new_state)
# Add states column to the DataFrame
random_numbers["State"] = states
print(random_numbers)