1

Suppose I've a list of data that contains multiple row values for multiple columns:

data = ['jack', 34, 'Sydney', 155 , 'Riti', 31, 'Delhi', 177.5 , 'Aadi', 16, 'Mumbai', 81 , 
        'Mohit', 31, 'Delhi', 167 , 'Veena', 12, 'Delhi', 144 , 'Shaunak', 35, 'Mumbai', 135 ,
        'Shaun', 35, 'Colombo', 111]

And I've another list that contains the name of columns:

columns = ['Name', 'Age', 'City', 'Score']

Now how can I merge these two lists into a single dataframe with pandas, like this?

expected output here

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
spyderB
  • 59
  • 5

3 Answers3

0

You can create np.array from your list and reshape it afterwards using .reshape:

df = pd.DataFrame(np.array(data).reshape((-1, len(columns))), columns=columns)
print(df)

Prints:

      Name Age     City  Score
0     jack  34   Sydney    155
1     Riti  31    Delhi  177.5
2     Aadi  16   Mumbai     81
3    Mohit  31    Delhi    167
4    Veena  12    Delhi    144
5  Shaunak  35   Mumbai    135
6    Shaun  35  Colombo    111
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

You can use data[0::4] to select every 4th item, starting from the first, data[1::4] to select every 4th item starting from the 2nd, and so on. Then, put those into an array and use dict + zip with that array and the columns and pass that to pd.DataFrame:

df = pd.DataFrame(dict(zip(columns, [data[0::4], data[1::4], data[2::4], data[3::4]])))

Output:

>>> df
      Name  Age     City  Score
0     jack   34   Sydney  155.0
1     Riti   31    Delhi  177.5
2     Aadi   16   Mumbai   81.0
3    Mohit   31    Delhi  167.0
4    Veena   12    Delhi  144.0
5  Shaunak   35   Mumbai  135.0
6    Shaun   35  Colombo  111.0
  • 1
    Apparently this one is a common question and I answered one [here](https://stackoverflow.com/a/72007753/17521785) using a similar idea as yours –  Apr 27 '22 at 19:22
  • 1
    @enke Interesting. So this is a dupe. –  Apr 27 '22 at 19:23
0
import pandas as pd

data = ['jack', 34, 'Sydney', 155, 'Riti', 31, 'Delhi', 177.5, 'Aadi', 16, 'Mumbai', 81, 'Mohit', 31, 'Delhi', 167,
        'Veena', 12, 'Delhi', 144, 'Shaunak', 35, 'Mumbai', 135, 'Shaun', 35, 'Colombo', 111]

columns = ['Name', 'Age', 'City', 'Score']
nnn = []
for i in range(0, len(data), 4):
    nnn.append(data[i:i + 4])

df = pd.DataFrame(nnn, columns=columns)

Output

      Name  Age     City  Score
0     jack   34   Sydney  155.0
1     Riti   31    Delhi  177.5
2     Aadi   16   Mumbai   81.0
3    Mohit   31    Delhi  167.0
4    Veena   12    Delhi  144.0
5  Shaunak   35   Mumbai  135.0
6    Shaun   35  Colombo  111.0
inquirer
  • 4,286
  • 2
  • 9
  • 16