Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested List to Pandas Dataframe with headers

Tags:

python

pandas

Basically I am trying to do the opposite of How to generate a list from a pandas DataFrame with the column name and column values?

To borrow that example, I want to go from the form:

data = [['Name','Rank','Complete'],
               ['one', 1, 1],
               ['two', 2, 1],
               ['three', 3, 1],
               ['four', 4, 1],
               ['five', 5, 1]]

which should output:

 Name Rank Complete
  One    1        1
  Two    2        1
Three    3        1
 Four    4        1
 Five    5        1

However when I do something like:

pd.DataFrame(data)

I get a dataframe where the first list should be my colnames, and then the first element of each list should be the rowname

EDIT:

To clarify, I want the first element of each list to be the row name. I am scrapping data so it is formatted this way...

like image 921
qwertylpc Avatar asked Sep 30 '15 03:09

qwertylpc


People also ask

How do I convert a nested list to a DataFrame in Python?

Practical Data Science using PythonWe first take the list of nested dictionary and extract the rows of data from it. Then we create another for loop to append the rows into the new list which was originally created empty. Finally we apply the DataFrames function in the pandas library to create the Data Frame.

How do I add a header to a pandas DataFrame?

You can add header to pandas dataframe using the df. colums = ['Column_Name1', 'column_Name_2'] method.

Can you put a list in a pandas DataFrame?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame.

How do I make a nested list one list in Python?

The task is to convert a nested list into a single list in python i.e no matter how many levels of nesting is there in the python list, all the nested have to be removed in order to convert it to a single containing all the values of all the lists inside the outermost brackets but without any brackets inside.


1 Answers

One way to do this would be to take the column names as a separate list and then only give from 1st index for pd.DataFrame -

In [8]: data = [['Name','Rank','Complete'],
   ...:                ['one', 1, 1],
   ...:                ['two', 2, 1],
   ...:                ['three', 3, 1],
   ...:                ['four', 4, 1],
   ...:                ['five', 5, 1]]

In [10]: df = pd.DataFrame(data[1:],columns=data[0])

In [11]: df
Out[11]:
    Name  Rank  Complete
0    one     1         1
1    two     2         1
2  three     3         1
3   four     4         1
4   five     5         1

If you want to set the first column Name column as index, use the .set_index() method and send in the column to use for index. Example -

In [16]: df = pd.DataFrame(data[1:],columns=data[0]).set_index('Name')

In [17]: df
Out[17]:
       Rank  Complete
Name
one       1         1
two       2         1
three     3         1
four      4         1
five      5         1
like image 135
Anand S Kumar Avatar answered Oct 27 '22 00:10

Anand S Kumar