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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With