Pyd
Pyd

Reputation: 6159

Get one or more column values as a list from Pandas Dataframe

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]

Upvotes: 1

Views: 606

Answers (5)

BENY
BENY

Reputation: 323226

By using melt

df[['Bin1','Bin3']].melt().value.tolist()
Out[382]: ['A', 'B', 'C', 7, 8, 9]

Upvotes: 1

Jundiaius
Jundiaius

Reputation: 7630

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

Upvotes: 0

Zero
Zero

Reputation: 76917

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

Upvotes: 1

jezrael
jezrael

Reputation: 862631

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()

Upvotes: 1

Evan Nowak
Evan Nowak

Reputation: 895

Here you go:

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

['A', 'B', 'C', 7, 8, 9]

Upvotes: 1

Related Questions