0

I want to extract the entire row values as list from df when column equal something.

I tried

df.loc['column'== x]

but it gives the column headers and not a list

Basically I want is to parse through each row in df and get the entire row as list when df['column']==x .The column I am parsing is the first column (company name). For company x I would like to get the list of the values in all columns (I don't want the company name and the column names to be in the list. I just want the values). ps:There are no duplicates in the company names

K241991
  • 23
  • 1
  • 6
  • 1
    df.loc[:, 'column'== x] .tolist() – BENY Aug 25 '19 at 16:57
  • I think you need to clarify your question. – Benoit Drogou Aug 25 '19 at 18:11
  • Basically I want is to parse through each row in df and get the entire row as list when df['column']==x .The column I am parsing is the first column (company name). For company x I would like to get the list of the values in all columns (I don't want the company name and the column names to be in the list. I just want the values). ps:There are no duplicates in the company names – K241991 Aug 25 '19 at 18:29

1 Answers1

0

Use .loc() to get only the rows that you are interested in. Then turn your Dataframe into a list of lists. So you need tyo get the values and turn them into list with tolist() method in Series.

You probably want to use:

df.loc[df['column']==x].values.tolist()

Have a look at this link

Here is an example:

In [1]:
import pandas as pd

ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
   'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
   'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
   'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
   'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df = pd.DataFrame(ipl_data)
df.loc[df['Team']=='Riders'].values.tolist()

Out [1]:
[['Riders', 1, 2014, 876],
 ['Riders', 2, 2015, 789],
 ['Riders', 2, 2016, 694],
 ['Riders', 2, 2017, 690]]
Benoit Drogou
  • 969
  • 1
  • 5
  • 15
  • df.loc[df['column']==x, 'column'].tolist() – K241991 Aug 25 '19 at 18:21
  • Basically I want is to parse through each row in df and get the entire row as list when df['column']==x .The column I am parsing is the first column (company name). For company x I would like to get the list of the values in all columns (I don't want the company name and the column names to be in the list. I just want the values). ps:There are no duplicates in the company names – K241991 Aug 25 '19 at 18:34
  • Okay, answer just edited. I think this is what you want – Benoit Drogou Aug 25 '19 at 22:45
  • Out [1]: [[ 1, 2014, 876], [ 2, 2015, 789], [ 2, 2016, 694], [ 2, 2017, 690]] How can I get this output? Thank you! – K241991 Aug 25 '19 at 23:48
  • Your answer works best. But I don't want the first column (team name in your example) – K241991 Aug 26 '19 at 00:06
  • okay, then add `.drop(columns=['Team'], axis=1, inplace=True)` right before `.values.tolist()` – Benoit Drogou Aug 26 '19 at 18:53