Skip to content Skip to sidebar Skip to footer

Convert Lists From Separate Variables Into A Dataframe

If I have 5 separate lists from different variable as below. How can I convert them to a pandas dataframe. a = [1.4, 1.3] b = [0.8, 0.8] c = [2.4, 1.6] d = [3.6, 2.9] e = [2.8, 2.5

Solution 1:

Use pd.DataFrame to convert your list to a df:

ls = [[1.4, 1.3],
      [0.8, 0.8],
      [2.4, 1.6],
      [3.6, 2.9],
      [2.8, 2.5]]

df = pd.DataFrame(ls,columns=['x','y'])

Solution 2:

Here is how:

import pandas as pd

df = [[1.4, 1.3],
      [0.8, 0.8],
      [2.4, 1.6],
      [3.6, 2.9],
      [2.8, 2.5]]

df = pd.DataFrame(df, columns=['x', 'y']).to_string(index=False)

print(df)

Output:

   x    y
 1.4  1.3
 0.8  0.8
 2.4  1.6
 3.6  2.9
 2.8  2.5

Post a Comment for "Convert Lists From Separate Variables Into A Dataframe"