1

For the given dataframe

  Bin1  Bin2  Bin3
0    A     1     7
1    B     2     8
2    C     3     9

I want a list of values from Bin1 and Bin3 columns

I tried,

df[["Bin1","Bin3"]].values.tolist()

But it is not giving the expected list.

My desired output is,

output_df = ["A","B","C",7,8,9]
Zero
  • 74,117
  • 18
  • 147
  • 154
Pyd
  • 6,017
  • 18
  • 52
  • 109

5 Answers5

2

Here you go:

df['Bin1'].tolist() + df['Bin3'].tolist()

['A', 'B', 'C', 7, 8, 9]
Evan Nowak
  • 895
  • 4
  • 8
1

Create list of lists and then flatten:

l = df[["Bin1","Bin3"]].values.T.tolist()

flat_list = [item for sublist in l for item in sublist]
print (flat_list)
['A', 'B', 'C', 7, 8, 9]

Similar, thanks Bharath shetty:

flat_list = df[["Bin1","Bin2"]].values.T.flatten().tolist()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Few other ways

Option 1 unstack

In [1413]: df[['Bin1', 'Bin3']].unstack().values.tolist()
Out[1413]: ['A', 'B', 'C', 7L, 8L, 9L]

Option 2 ravel

In [1426]: df[['Bin1', 'Bin3']].values.ravel(order='A')
Out[1426]: array(['A', 'B', 'C', 7L, 8L, 9L], dtype=object)

Timings

In [1446]: df.shape
Out[1446]: (60000, 3)

In [1447]: %timeit df['Bin1'].values.tolist() + df['Bin3'].values.tolist()
100 loops, best of 3: 2.95 ms per loop

In [1440]: %timeit df['Bin1'].tolist() + df['Bin3'].tolist()
100 loops, best of 3: 4.87 ms per loop

In [1442]: %timeit df[['Bin1', 'Bin3']].values.ravel(order='A').tolist()
100 loops, best of 3: 5.86 ms per loop

In [1443]: %timeit df[['Bin1', 'Bin3']].unstack().values.tolist()
100 loops, best of 3: 9.32 ms per loop

In [1444]: %timeit df[["Bin1","Bin2"]].values.T.flatten().tolist()
100 loops, best of 3: 6.91 ms per loop

In [1445]: %timeit [it for subl in df[["Bin1","Bin3"]].values.T.tolist() for it in subl]
10 loops, best of 3: 20.3 ms per loop
Zero
  • 74,117
  • 18
  • 147
  • 154
1

By using melt

df[['Bin1','Bin3']].melt().value.tolist()
Out[382]: ['A', 'B', 'C', 7, 8, 9]
BENY
  • 317,841
  • 20
  • 164
  • 234
0

As easy as: list(df[["Bin1","Bin2"]].as_matrix().flatten())

kvorobiev
  • 5,012
  • 4
  • 29
  • 35
Jundiaius
  • 6,214
  • 3
  • 30
  • 43