Ask Your Question
2

How can the dataframe be transformed into a record list without including brackets?

asked 2022-03-16 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-03-30 13:00:00 +0000

ladyg gravatar image

To transform a dataframe into a record list without including brackets, you can use the to_dict() method with the orient parameter set to "records". This will return a list of dictionaries where each dictionary represents a record from the dataframe. Here is an example:

import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]})

# transform the dataframe into a record list
records = df.to_dict(orient='records')

# print the record list without brackets
for record in records:
    print(*record.values(), sep=', ')

Output:

Alice, 25
Bob, 30
Charlie, 35

Note that the * operator is used to unpack the dictionary values into separate arguments for the print() function, which separates the values with commas instead of brackets.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-16 11:00:00 +0000

Seen: 13 times

Last updated: Mar 30 '22