0

I have the following list:

x=['2 5 6 7', '9 11 13 15', '31 52 56 94']

My ultimate goal is to display it as table with columns and rows in jupyter notebook.

This is what I have done so far:

  1. I converted the list into list of lists and now I have list looking like this:

    x=[['2 5 6 7'], ['9 11 13 15'], ['31 52 56 94']]

  2. The problem is that I am stuck how to split each string. For example I want to do get:

    [['2' '5' '6' '7'], ['9' '11' '13' '15'], ['31' '52' '56' '94']]

so that I can use the following to print a table using pandas library

import pandas as pd
df = pd.DataFrame (x,columns=['int1','int2','int3','int4'])
print (df)

final output should look like this with columns names and row names

int1 int2 int3 int4
   2    5    6    7
   9   11   13   15
  31   52   56   94

Please help me go in the right direction.

bb1
  • 7,174
  • 2
  • 8
  • 23

1 Answers1

2

Try:

x=['2 5 6 7', '9 11 13 15', '31 52 56 94']
pd.DataFrame([s.split() for s in x], columns=['int1', 'int2', 'int3', 'int4'])

It gives:

  int1 int2 int3 int4
0    2    5    6    7
1    9   11   13   15
2   31   52   56   94
bb1
  • 7,174
  • 2
  • 8
  • 23