1

I have a for loop that iterates over a dataframe and calculates two pieces of information:

for id in members['id']
    x = random_number_function()
    y = random_number_function()

I'd like to store id, x and y in a dataframe that is built one row at a time, for each pass through the for loop.

7
  • Is there a reason you want to build a dataframe one row at a time, rather than store info in a dict and then turn the dict into a data frame afterward? Commented Apr 26, 2015 at 22:07
  • Why can't you just do members['x'] = calculate_x(members['id']) and again for the 'y' column? Commented Apr 26, 2015 at 22:09
  • Not at all - I am unfamiliar with the dict object Commented Apr 26, 2015 at 22:09
  • You can just modify your calculate_ code to accept a Series so it's vectorised Commented Apr 26, 2015 at 22:10
  • 1
    but you can generate a random array of values using any number of np.random.randint or whatever functions, it's really unnecessary generally to ever loop row-wise Commented Apr 26, 2015 at 22:13

1 Answer 1

4

Here's an example of using a dict to build a dataframe:

dict_for_df = {}
for i in ('a','b','c','d'):    # Don't use "id" as a counter; it's a python function
    x = random.random()        # first value
    y = random.random()        # second value
    dict_for_df[i] = [x,y]     # store in a dict
df = pd.DataFrame(dict_for_df) # after the loop convert the dict to a dataframe
Sign up to request clarification or add additional context in comments.

2 Comments

This works - although I was getting my results transposed the wrong way. Instead of df=pd.DataFrame(dict_for_df) I used, df = DataFrame.from_dict(dict_for_df, orient='index')
Or just use "pd.DataFrame(dict_for_df).T" -- the "T" transposes the dataframe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.