简体   繁体   中英

creating a list of tuples from dataframe entries

I have a dataframe df :

movie_title          director_name     ...   oscar_wins   oscar_nominees
El Mariachi          Robert Rodriguez  ...       0             0
My Date with Drew    Jon Gunn          ...       0             0

What I want to do is create a list of tuples where every tuple is one row of the dataframe. So the output would have to look like this:

[(El Mariachi, Robert Rodriguez, ... , 0, 0), (My Date with Drew, Jon Gunn, ..., 0, 0) ...]

I've tried iterating over both the length and column names among other things with no succes.

list(zip(range(len(df)), column_names)

Although I know why it does not work, I'm not sure how to achieve what I want. Is there anybody that can help me or provide me with a fix?

Thanks and much appreciated!

Solution with list comprehension:

L = [tuple(x) for x in df.values.tolist()]
print (L)
[('El Mariachi', 'Robert Rodriguez', 0, 0), ('My Date with Drew', 'Jon Gunn', 0, 0)]

Just return the values from the list and map the inner lists to tuples:

list(map(tuple,df.values.tolist()))
# [('El Mariachi', 'Robert Rodriguez', 0, 0), ('My Date with Drew', 'Jon Gunn', 0, 0)]

You can also do this:

[tuple(x) for x in df.to_numpy()]
# [('El Mariachi', 'Robert Rodriguez', 0, 0), ('My Date with Drew', 'Jon Gunn', 0, 0)]

The fastest method would be by using to_records() :

df.to_records(index=False).tolist()
 all_rows=[]
 for index, row in df.iterrows():
        all_rows.append(tuple(row))

Explanation: Using the for loop & iterrows(), one can iterate over a dataframe. The two elements index(represents index of df) & row(depicts row of a sample which is a list). Now using tuple(), convert this row(list type object) to tuple & append in a new list 'all_rows'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM