I have a dataframe and I want to create and populate the column with values inside the function process()
.
import random
import pandas as pd
df = pd.DataFrame()
def process():
global df
df['z'] = random.randint(0, 100)
for i in range(5):
process()
print(df)
The expected output:
z
0 21
1 83
2 29
3 10
4 43
Currently I get an empty dataframe with column z
printed.
Update:
The following line will create and populate the column values.
df.loc[len(df), 'z'] = random.randint(0, 100)
CodePudding user response:
It looks like what you want could be simplified to:
def process():
return random.randint(0, 100)
df = pd.DataFrame({'z': [process() for i in range(5)]})
example output:
z
0 53
1 68
2 45
3 56
4 23
CodePudding user response:
I assume you just need to create a new column with 5 values, if there's any updates from your questions, feel free to comment.
import random
import pandas as pd
df = pd.DataFrame()
def process(column):
global df
df[column]= [random.randint(0,100) for i in range(5)]
process("z")
print(df)
z
0 20
1 85
2 43
3 35
4 55