Python create dataframe from list


Asked by wiki @ & viewed by 124 persons


Python: create a pandas data frame from a list

I am using the following code to create a data frame from a list:

test_list = ['a','b','c','d']
df_test = pd.DataFrame.from_records(test_list, columns=['my_letters'])
df_test

The above code works fine. Then I tried the same approach for another list:

import pandas as pd
q_list = ['112354401', '116115526', '114909312', '122425491', '131957025', '111373473']
df1 = pd.DataFrame.from_records(q_list, columns=['q_data'])
df1

But it gave me the following errors this time:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-24-99e7b8e32a52> in <module>()
      1 import pandas as pd
      2 q_list = ['112354401', '116115526', '114909312', '122425491', '131957025', '111373473']
----> 3 df1 = pd.DataFrame.from_records(q_list, columns=['q_data'])
      4 df1

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in from_records(cls, data, index, exclude, columns, coerce_float, nrows)
   1021         else:
   1022             arrays, arr_columns = _to_arrays(data, columns,
-> 1023                                              coerce_float=coerce_float)
   1024 
   1025             arr_columns = _ensure_index(arr_columns)

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _to_arrays(data, columns, coerce_float, dtype)
   5550         data = lmap(tuple, data)
   5551         return _list_to_arrays(data, columns, coerce_float=coerce_float,
-> 5552                                dtype=dtype)
   5553 
   5554 

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _list_to_arrays(data, columns, coerce_float, dtype)
   5607         content = list(lib.to_object_array(data).T)
   5608     return _convert_object_array(content, columns, dtype=dtype,
-> 5609                                  coerce_float=coerce_float)
   5610 
   5611 

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _convert_object_array(content, columns, coerce_float, dtype)
   5666             # caller's responsibility to check for this...
   5667             raise AssertionError('%d columns passed, passed data had %s '
-> 5668                                  'columns' % (len(columns), len(content)))
   5669 
   5670     # provide soft conversion of object dtypes

AssertionError: 1 columns passed, passed data had 9 columns

Why would the same approach work for one list but not another? Any idea what might be wrong here? Thanks a lot!

Do you know the better answer?

Similar questions

Create dataframe in python from list

Asked by wiki @ & viewed by 57 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

Create a dataframe in python from list

Asked by wiki @ & viewed by 60 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

Create a dataframe from a list python

Asked by wiki @ & viewed by 59 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

Python create dataframe from two lists

Asked by wiki @ & viewed by 62 persons

Take multiple lists into dataframe How do I take multiple lists and put them as different columns in a python …

Create dataframe using list python

Asked by wiki @ & viewed by 60 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

Python create dataframe from scratch

Asked by wiki @ & viewed by 4 persons

Creating an empty Pandas DataFrame, then filling it? I'm starting from the pandas DataFrame docs here: http://pandas.pydata.org/pandas-docs/stable/dsintro.html I'd like to …

Create subset of dataframe in python

Asked by wiki @ & viewed by 5 persons

subsetting a Python DataFrame I am transitioning from R to Python. I just began using Pandas. I have an R …

Create table from list python

Asked by wiki @ & viewed by 6 persons

Printing Lists as Tabular Data I am quite new to Python and I am now struggling with formatting my data …

Create a list of tuples python

Asked by wiki @ & viewed by 7 persons

python: create list of tuples from lists [duplicate] This question already has answers here: Closed 9 years ago. I have …

How to create a list from a string in python

Asked by wiki @ & viewed by 9 persons

python: creating list from string [duplicate] This question already has answers here: How to convert string representation of list to …

Python create 2d list

Asked by wiki @ & viewed by 11 persons

How to define a two-dimensional array? I want to define a two-dimensional array without an initialized length like this: Matrix …

Python append or create list

Asked by wiki @ & viewed by 10 persons

Create or append to a list in a dictionary - can this be shortened? Can this Python code be shortened …

Python create dataframe from array

Asked by wiki @ & viewed by 12 persons

Create a dataframe from arrays python I'm try to construct a dataframe (I'm using Pandas library) from some arrays and …

Transform list to dataframe python

Asked by wiki @ & viewed by 15 persons

Convert list into a pandas data frame I am trying to convert my output into a pandas data frame and …

Python dataframe to list of tuples

Asked by wiki @ & viewed by 17 persons

Pandas convert dataframe to array of tuples I have manipulated some data using pandas and now I want to carry …

Most viewed questions


Python strftime milliseconds

Asked by wiki @ & viewed by 200 persons

Format a datetime into a string with milliseconds I want to have a datetime string from the date with milliseconds. …

What does the percent sign mean in python

Asked by wiki @ & viewed by 136 persons

What does the percentage sign mean in Python In the tutorial there is an example for finding prime numbers: >>> …

Self keyword in python

Asked by wiki @ & viewed by 134 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Check object type python

Asked by wiki @ & viewed by 132 persons

Determine the type of an object? Is there a simple way to determine if a variable is a list, dictionary, …

Alphabetical sort python

Asked by wiki @ & viewed by 131 persons

Python data structure sort list alphabetically I am a bit confused regarding data structure in python; (),[], and {}. I …

Correlation matrix python

Asked by wiki @ & viewed by 131 persons

Plot correlation matrix using pandas I have a data set with huge number of features, so analysing the correlation matrix …

Python random

Asked by wiki @ & viewed by 127 persons

Generate random integers between 0 and 9 How can I generate random integers between 0 and 9 (inclusive) in Python? …

Python create dataframe from list

Asked by wiki @ & viewed by 124 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

Python one line for loop

Asked by wiki @ & viewed by 123 persons

Python one-line “for” expression I'm not sure if I need a lambda, or something else. But still, I need the …

How to add a column to dataframe python

Asked by wiki @ & viewed by 123 persons

Adding new column to existing DataFrame in Python pandas I have the following indexed DataFrame with named columns and rows …

Why we use self in python

Asked by wiki @ & viewed by 115 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Shape 0 python

Asked by wiki @ & viewed by 115 persons

x.shape[0] vs x[0].shape in NumPy Let say, I have an array with x.shape = (10,1024) when I try to print …

Python print working directory

Asked by wiki @ & viewed by 112 persons

Find current directory and file's directory [duplicate] This question already has answers here: How do you properly determine the current …

Image shape python

Asked by wiki @ & viewed by 110 persons

Python OpenCV2 (cv2) wrapper to get image size? How to get the size of an image in cv2 wrapper in …

Interface grafica python

Asked by wiki @ & viewed by 108 persons

Como criar interface gráfica em Python? [duplicada] Essa pergunta já tem uma resposta aqui: É possível criar interfaces gráficas para …