Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list into a pandas data frame

Tags:

python

pandas

I am trying to convert my output into a pandas data frame and I am struggling. I have this list

my_list = [1,2,3,4,5,6,7,8,9] 

I want to create a pandas data frame that would have 3 columns and three rows. I try using

df = pd.DataFrame(my_list, columns = list("abc")) 

but it doesn't seem to be working for me. Any help would be appreciated.

like image 791
Kay Avatar asked Mar 04 '17 06:03

Kay


People also ask

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

Method 1: Using T function This is known as the Transpose function, this will convert the list into a row. Here each value is stored in one column. Example: Python3.

How do you pass a list into a DataFrame?

Convert Lists of tuples to DataFrame in Pandas Just like list of lists we can pass list of tuples in dataframe constructor to create a dataframe. Suppose we have a list of tuples i.e. Pass this list of tuples to DataFrame's constructor to create a DataFrame object i.e. Both Column & Index labels are default.

Can we create DataFrame from list?

The pandas DataFrame can be created by using the list of lists, to do this we need to pass a python list of lists as a parameter to the pandas. DataFrame() function. Pandas DataFrame will represent the data in a tabular format, like rows and columns.


1 Answers

You need convert list to numpy array and then reshape:

df = pd.DataFrame(np.array(my_list).reshape(3,3), columns = list("abc")) print (df)    a  b  c 0  1  2  3 1  4  5  6 2  7  8  9 
like image 50
jezrael Avatar answered Sep 22 '22 09:09

jezrael