Home > OS >  How do I get values by rows in a data frame python
How do I get values by rows in a data frame python

Time:11-21

I have a huge data frame with several columns and rows, and I would like to get all values by rows skipping the first column.

import pandas as pd
df = pd.DataFrame([['A', 2, 4, 7], ['B', 6, 1, 5], ['C', 4, 2, 2], ['D', 3, 9, 8]], columns = ["Pen", "A", 'B', 'C'])
    
values = []
for row in df.iterrows(0, 1):
    values. Append(values)

print(values)

expected:

[2, 4, 7, 6, 1, 5, 4, 2, 2, 3, 9, 8]

My code does not work.

CodePudding user response:

You can use pandas.DataFrame.select_dtypes to select only the numeric columns then pandas.DataFrame.stack with pandas.Series.tolist to make a list of every number found :

import numpy as np

out= df.select_dtypes(include=np.number).stack().to_list()

# Output :

print(out)

[2, 4, 7, 6, 1, 5, 4, 2, 2, 3, 9, 8]

CodePudding user response:

you can use stack():

vals=list(df.iloc[:,1:].stack())
#[2, 4, 7, 6, 1, 5, 4, 2, 2, 3, 9, 8]

CodePudding user response:

If you want a list, get rid of the Pen column and stack, then convert the Series to_list:

l = df.set_index('Pen').stack().tolist()

Output: [2, 4, 7, 6, 1, 5, 4, 2, 2, 3, 9, 8]

CodePudding user response:

Let us try

out = df.iloc[:,1:].to_numpy().ravel().tolist()
#[2, 4, 7, 6, 1, 5, 4, 2, 2, 3, 9, 8]
  • Related