0

I have a more complicated code but I just created this simple example to explain what I need to do.

for i in np.arange(0,360): 
    r = 2*i
    print(r)
    d = {'r': [r]}
    df = pd.DataFrame(data=d)

Instead of printing r, how would I save r into the dataframe df? I tried to replicate an example from the documentation for dataframes but I'm lost how to properly add every iteration of r to the dataframe.

3
  • It's not clear what you intend, because df is a dict, not a data frame. Regardless of the terminology, you're asking us how to assign to a well-documented data structure -- just what confuses you from the tutorials and examples on your data type? Commented Aug 3, 2021 at 19:20
  • I apologize. Please see my edit to the original post. Commented Aug 3, 2021 at 19:26
  • Please repeat on topic and how to ask from the intro tour. You're still confusing the two data structures. On each loop, you set d to a single-element dict, and then convert that to a single-row data frame, overwriting the original data frame. Please work through a tutorial on data frames to see how to initialize them from various sources. Commented Aug 3, 2021 at 20:54

1 Answer 1

1

You could store the it as key-value pairs where the key will the value of i and r will be it's 2*i.

for i in np.arange(0,360): 
    r = 2*i
    df[i] = r

If you want you can also combine this dict into your existing dataframe.

Sign up to request clarification or add additional context in comments.

Comments

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.