1 | initial version |
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.