0

I'm trying to convert a list into a dataframe, but this list was created by another dataframme, so it has index without header in it, and I'm trying to convert it into another df and failing.

id_range = [100,200,300,400,400]

summary df : a normal df with an index

    Name ID 
0   Paul    100  
1   Lauren  200  
2   Beth    300 
3   Chris   400  
4   Andy    500  

list : the df converted into a list with a new column added(it has index without header\column name)

    Name Surname ID 
0   Paul Logan 100  
1   Lauren Text 200  
2   Beth Cameron 300 
3   Chris Lenon 400  
4   Andy Steve 500 

new_df : what im trying to do, convert the list into this new_df to make another operation using a df

could you guys help me?

I tried

new_df = pd.DataFrame(list) 

but this doesnt give me everything, just one row

gfernandes
  • 193
  • 1
  • 10
  • 2
    Please edit your question to include example data following the instructions in [this post](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Without data, nobody is going to be able to help you. – fsimonjetz Aug 22 '22 at 13:23
  • share your `list`. Check-https://stackoverflow.com/help/how-to-ask – Divyank Aug 22 '22 at 13:24
  • This is probably a duplicate of [Add column in dataframe from list](https://stackoverflow.com/questions/26666919/add-column-in-dataframe-from-list) – Ingebrigt Nygård Aug 22 '22 at 13:58
  • If it's possible, can you also post your desired output? –  Aug 22 '22 at 14:15
  • a new df from the mentioned list, simple as that.. – gfernandes Aug 22 '22 at 17:50

1 Answers1

1

list : the df converted into a list with a new column added(it has index without header\column name)

Considering list should be as below after conversion of df to list and added surname in list.

USE df_new = pd.DataFrame(list[1:],columns=list[0]) to get df_new, first element in list will act as Column name in df_new

import pandas as pd
  
list =  [['Name','Surname','ID'],['Paul', 'Logan', 100],['Louren','Text',200],["Beth","Camron",300],["Chris","Lenon",400],["Andy","Steve",500]] 
df_new = pd.DataFrame(list[1:],columns=list[0])
df_new

Output-

    Name    Surname ID
0   Paul    Logan   100
1   Louren  Text    200
2   Beth    Camron  300
3   Chris   Lenon   400
4   Andy    Steve   500
Divyank
  • 811
  • 2
  • 10
  • 26
  • thanks for your reply, I tried that, but, look what I got: ValueError: Shape of passed values is (1, 1), indices imply (12, 1) and the command I tried was : df_final = pd.DataFrame(list_dados[1:],columns=list_dados[0]) – gfernandes Aug 22 '22 at 17:44
  • @gfernandes then your `list` is not similar with the `list` I am using in the answer, you will require `list` to be as above to work – Divyank Aug 22 '22 at 18:49