Search code examples
pythonpandasdataframerows

Selecting values from pandas rows


Hello I want to make a for loop that will get all the values from each row and dump it into a list. I want the list to overwrite on the same variable.

I made something like this but there is now way i can figure out how to print or append only row values.

rows_count = len(df.index)

for i in range(rows_count):
    extracted_row = df.iloc[[0]]
    print(extracted_row)
    if i == 1:
        break

I tried to make a for loop over extracted_row but it does not work.

This is the result I am getting but i'd like it to be just a list of values

Instead of this: enter image description here

I'd like to get this:

[1,28,2,208,...0,6,4]

Solution

  • You can reach the expected output using values :

    rows_count = len(df.index)
    
    for i in range(rows_count):
        extracted_row = df.iloc[[i]].values[0].tolist()
        print(extracted_row)
        if i == 1:
            break
    

    Output :

    [-0.4638010118887267, 0.8128665595646368, 0.10555011868229829, -1.0352554514123276]