Create dataframe tuples generated by Pandas

Asked

Viewed 741 times

0

Is it possible to generate a tuple that stores the values of the lines of a dataframe generated by pandas? I uploaded the values of a CSV file to a dataframe, now I need to perform some calculations with these values, but I did not find hints on the internet. Thanks.

For example: tuple = "value","valueB".

Where tuple is the column name.

valora is the value contained in first row of this column.

valorB is the heat contained in second row of this column.

And so on and so forth while there are lines in the dataframe.

 import pandas as pd

    df = pd.read_csv("TESTE.CSV", sep=",",header=None,usecols=[0,1,2,3,4,5,6],engine="python")

    print (df)

1 answer

2


Good afternoon

You can do it this way:

tuples = [tuple(x) for x in df.values]

For you to take the tuples per column would be something like this:

colunas = df.columns.values
tuples = [tuple(x) for x in [df[coluna].values for coluna in colunas]]
  • Oops, all right? So I store all the values from df, right? There’s a way to create a tuple for each column?

  • Right, so it stores all the data in the column at the address 0 of the tuple. Is there any way to store the data in the column at different addresses? for example: tuple[0] contains the die of row 0, tuple[1] contains the die of row 1 and so on until the column is finished.

  • In fact the structure of tuples is as follows: tuples[coluna][linha], for example tuples[0][1] will return the value of row 1 and column 0, the value of tuples[1][1] will return the value of row 1 and column 1 and tuples[0][3] will return the value of row 3 and column 0

  • Right, helped me a lot! Thanks Brow-joe!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.