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.
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
Asked: 2022-03-16 11:00:00 +0000
Seen: 13 times
Last updated: Mar 30 '22
What causes a syntax error in a for loop and array in Pine Script?
Investigate the usage of square brackets.
How can brackets in a filepath be managed when it is passed as a variable in the pipeline in Bash?
What is the method for placing parentheses in column names when creating a table using an SQL query?
In R, how can one extract specific lines from a .txt file based on predetermined variable values?
What is the method for transferring values from JS to HTML tags in a Lightning Web Component?
What is the process for implementing BODMAS or BIDMAS in my Java calculator code?