How do i convert a list to a dataframe row in python?

In this article, we will discuss how to 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.

Syntax: pandas.DataFrame(list).T

Example:

Python3

import pandas as pd

list1 = ["durga", "ramya", "meghana", "mansa"]

data = pd.DataFrame(list1).T

data.columns = ['student1', 'student2',

                'student3', 'student4']

data

Output:

How do i convert a list to a dataframe row in python?

Method 2: Creating from multi-dimensional list to dataframe row

Here we are converting a list of lists to dataframe rows

Syntax: pd.DataFrame(list)

where list is the list of lists

Example:

Python3

import pandas as pd

list1 = [["durga", "java", 90], ["gopi", "python", 80],

         ["pavani", "c/cpp", 94], ["sravya", "html", 90]]

data = pd.DataFrame(list1)

data.columns = ['student1', 'subject', 'marks']

data

Output:

Method 3: Using a list with index and columns

Here we are getting data (rows ) from the list and assigning columns to these values from columns 

Syntax: pd.DataFrame(list, columns, dtype )

where

  • list is  the list of input values
  • columns are the column names for list of values
  • dtype is the column data type

Example:

Python3

import pandas as pd

list1 = [["durga", "java", 90], ["gopi", "python", 80],

         ["pavani", "c/cpp", 94], ["sravya", "html", 90]]

data = pd.DataFrame(list1, columns=['student1',

                                    'subject',

                                    'marks'])

data

Output:

Method 4: Using zip() function

Here  we are taking separate lists as input such that each list will act as one column, so the number of lists = n columns in the dataframe, and using zip function we are combining the lists.

Syntax pd.DataFrame(list(zip(list1,list2,.,list n)),columns)

where

  • columns is the column for the list values
  • list1.list n represent number of input lists for columns

Example:

Python3

import pandas as pd

list1 = ["durga", "ramya", "sravya"]

list2 = ["java", "php", "mysql"]

list3 = [67, 89, 65]

data = pd.DataFrame(list(zip(list1, list2, list3)),

                    columns=['student', 'subject', 'marks'])

data

Output:

Method 5: Using a list of dictionary

Here we are passing the individual lists which act as columns in the data frame to keys to the dictionary, so by passing the dictionary into dataframe() we can convert list to dataframe.

Syntax: pd.DataFrame{‘key’: list1, ‘key’: list2, ……..,’key’: listn}

These keys will be the column names in the dataframe.

Example:

Python3

import pandas as pd

list1 = ["durga", "ramya", "sravya"]

list2 = ["java", "php", "mysql"]

list3 = [67, 89, 65]

dictionary = {'name': list1, 'subject': list2,

              'marks': list3}

data = pd.DataFrame(dictionary)

data

Output:

Method 6: Creating from  multi-dimensional list to dataframe row with columns

Here we are taking input from multi-dimensional lists and assigning column names in the DataFrame() function

Syntax: pd.DataFrame(list,columns)

where

  1. list is an multidimensional list
  2. columns are the column names

Example:

Python3

import pandas as pd

list1 = [["durga", "java", 90],

         ["gopi", "python", 80],

         ["pavani", "c/cpp", 94],

         ["sravya", "html", 90]]

data = pd.DataFrame(list1, columns=['student1',

                                    'subject',

                                    'marks'])

data

Output:


How do you convert a list into a 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.

Can we convert list to DataFrame in Python?

As we all know that data in python is mostly provided in the form of a List and it is important to convert this list into a data frame. If you want to practice pandas skills then you can check out Pandas exercises for Beginners.

How convert column with list of values into rows in pandas DataFrame?

Apply pd.series to column B --> splits each list entry to a different row..
Melt this, so that each entry is a separate row (preserving index).
Merge this back on original dataframe..
Tidy up - drop unnecessary columns and rename the values column..

How do you append a list as a row to a DataFrame?

Use pandas..
df = pd. DataFrame([[1, 2], [3, 4]], columns = ["a", "b"]).
print(df).
to_append = [5, 6].
a_series = pd. Series(to_append, index = df. columns).
df = df. append(a_series, ignore_index=True).
print(df).