1

I have a simple list

Code to retrieve i, v list:

 for i, v in enumerate(list_name_1):
    print(i, v)

Output

    5 -100
    4 30
    0 -90
    1 -80
    3 100
    2 1000
    and so on

I wish to store all these in a dataframe.

The problem is I am doing lot of calculations on list_name_1 within the for loop, hence I need to catch i and v values in the for loop only. So I cant do a simple df=pd.DataFrame(list_name_1) for e.g.

I will need to do something like below:

df = []
 for i, v in enumerate(list_name_1):
        print(i, v)
        some code to add i,v iteratively in columns i and v of df
4
  • You say "...of a dataframe with column names i and v", but in your example, all entries in the list_name_1 are unique, so the DataFrame should have very many columns, and very few rows? Commented Oct 19, 2020 at 6:39
  • Hi In list we only have 2 types of values i and v. I want to convert these to a dataframe with 2 columns i and v. So df will have 2 columns and many rows Commented Oct 19, 2020 at 6:40
  • Bascially list is feature importance. i is feature number, v is feature's value.. So I want to get this data in a df and be able to do some operations on it in df format Commented Oct 19, 2020 at 6:41
  • I've added an answer that I think does what you want. Let me know if there are any unclarities Commented Oct 19, 2020 at 6:48

3 Answers 3

2

An idea is to create two lists, one which stores the calculated values of i, and one for v. So:

i_values = []
v_values = []
for i, v in enumerate(list_name_1):
    print(i, v)
    # Make calculations for i and v
    i_values.extend([calculated_i])
    v_values.extend([calculated_v])

df = pd.DataFrame(data={"i": i_values, "v": v_values})
Sign up to request clarification or add additional context in comments.

3 Comments

NameError: name 'calculated_i' is not defined error. I am getting this error
Works well, thanks. instead of calculated_i replaced with i
I was under the impression that you wanted to make calculations in the loop, that was why I called them calculated_i. But hey, if it works and you're satisfied its all good!
1

I think you can do that:

for i, v in enumerate(list_name_1):
    diction = {'i': i, 'v': v}
    df = df.append(diction, ignore_index=True)
    

I don't know how you are doing the calculation, but I think it is a good way to catch all values.

1 Comment

If list_name_1 is very long, the loop will become very slow and performance will deteriorate since df.append() is generally bad practice in loops. You can read more about why here: stackoverflow.com/questions/27929472/…
-2

I think you should use predifined syntax for this.Using for loops is really long way.

import pandas as pd

L = ["This","Is","A","List"]
M = ["This","Is","Also","List"]
#create new dataframe
df = pd.DataFrame({'col':L,'col2':M})
print (df)


           col  col2
       0  This  This
       1    Is    Is
       2     A  Also
       3  List  List

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.