2

I have two 2D list:

1. [['VDM:1', 'VDM:2', 'VDM:3', 'VDM:4', 'VDM:5'], ['MDM:1', 'MDM:2', 'MDM:3', 'MDM:4', 'MDM:5'], ['OM:1', 'OM:2', 'OM:3', 'OM:4', 'OM:5']]
2. [[9, 2, 0, 0, 1], [2, 6, 0, 3, 1], [2, 6, 0, 3, 1]]

I want to store these value in a dataset in the format:

Attribute:Value     Support
VDM:1               9
VDM:2               2
VDM:3               0
VDM:4               0
VDM:5               1
MDM:1               2
MDM:2               6
MDM:3               0
MDM:4               3
MDM:5               1
OM:1                2
OM:2                6
OM:3                0
OM:4                3
OM:5                1
vikrant rana
  • 4,509
  • 6
  • 32
  • 72

4 Answers4

1

Using itertools.chain

Ex:

import pandas as pd
from itertools import chain

Attribute = [['VDM:1', 'VDM:2', 'VDM:3', 'VDM:4', 'VDM:5'], ['MDM:1', 'MDM:2', 'MDM:3', 'MDM:4', 'MDM:5'], ['OM:1', 'OM:2', 'OM:3', 'OM:4', 'OM:5']]
Support = [[9, 2, 0, 0, 1], [2, 6, 0, 3, 1], [2, 6, 0, 3, 1]]

df= pd.DataFrame({"Attribute:Value": list(chain.from_iterable(Attribute)), "Support": list(chain.from_iterable(Support))})
print(df)

Output:

   Attribute:Value  Support
0            VDM:1        9
1            VDM:2        2
2            VDM:3        0
3            VDM:4        0
4            VDM:5        1
5            MDM:1        2
6            MDM:2        6
7            MDM:3        0
8            MDM:4        3
9            MDM:5        1
10            OM:1        2
11            OM:2        6
12            OM:3        0
13            OM:4        3
14            OM:5        1
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

Using np.concatenate flatten the list.

a = [['VDM:1', 'VDM:2', 'VDM:3', 'VDM:4', 'VDM:5'], ['MDM:1', 'MDM:2', 'MDM:3', 'MDM:4', 'MDM:5'], ['OM:1', 'OM:2', 'OM:3', 'OM:4', 'OM:5']]
s = [[9, 2, 0, 0, 1], [2, 6, 0, 3, 1], [2, 6, 0, 3, 1]]

a = np.concatenate(a)
s = np.concatenate(s)

df = pd.DataFrame({'Attribute:value': a, 'Support': s})

Output:

    Attribute:value Support
0   VDM:1           9
1   VDM:2           2
2   VDM:3           0
3   VDM:4           0
4   VDM:5           1
5   MDM:1           2
6   MDM:2           6
7   MDM:3           0
8   MDM:4           3
9   MDM:5           1
10  OM:1            2
11  OM:2            6
12  OM:3            0
13  OM:4            3
14  OM:5            1
Sociopath
  • 13,068
  • 19
  • 47
  • 75
1

One simple way is to flatten your list. You can do it with list comprehension (no extra modules needed). Here is one discussion on how to flatten a list.

Here the code:

# Import module
import pandas as pd

# Your data
attributs = [['VDM:1', 'VDM:2', 'VDM:3', 'VDM:4', 'VDM:5'], [
    'MDM:1', 'MDM:2', 'MDM:3', 'MDM:4', 'MDM:5'], ['OM:1', 'OM:2', 'OM:3', 'OM:4', 'OM:5']]
support = [[9, 2, 0, 0, 1], [2, 6, 0, 3, 1], [2, 6, 0, 3, 1]]

# Flatten the list
attributs_flatten = [item for sublist in attributs for item in sublist]
support_flatten = [item for sublist in support for item in sublist]

# create dataframe
df = pd.DataFrame({'Attributes:Value': attributs_flatten, "Support": support_flatten})

print(df)
#    Attributes:Value  Support
# 0             VDM: 1        9
# 1             VDM: 2        2
# 2             VDM: 3        0
# 3             VDM: 4        0
# 4             VDM: 5        1
# 5             MDM: 1        2
# 6             MDM: 2        6
# 7             MDM: 3        0
# 8             MDM: 4        3
# 9             MDM: 5        1
# 10             OM: 1        2
# 11             OM: 2        6
# 12             OM: 3        0
# 13             OM: 4        3
# 14             OM: 5        1
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
1

simplest approach would be,

pd.DataFrame(list(zip(sum(l1, []),sum(l2,[]))))

O/P:

        0  1
0   VDM:1  9
1   VDM:2  2
2   VDM:3  0
3   VDM:4  0
4   VDM:5  1
5   MDM:1  2
6   MDM:2  6
7   MDM:3  0
8   MDM:4  3
9   MDM:5  1
10   OM:1  2
11   OM:2  6
12   OM:3  0
13   OM:4  3
14   OM:5  1

Explanation, flatten both dataframes and perform zipping finally convert into dataframe

Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111