📅  最后修改于: 2023-12-03 15:04:21.693000             🧑  作者: Mango
Pandas is an open-source data manipulation and analysis library. It provides various data structures and functions needed to work on structured data seamlessly. One of the most useful functions of Pandas is DataFrame.to_string().
Pandas DataFrame.to_string() method has the following syntax.
DataFrame.to_string(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, line_width=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.') -> str
lambda x: str(x).upper()
as a value for the 'Name' key in the formatters
dictionary.Returns a string representation of the DataFrame.
Let's consider an example of how to use the to_string() method.
import pandas as pd
data = {'Name': ['Peter', 'John', 'Sara', 'David'], 'Age': [23, 34, 29, 41], 'City': ['Atlanta', 'Denver', 'Boston', 'Seattle'], 'Salary': [55000, 78000, 92000, 66000]}
df = pd.DataFrame(data)
print(df.to_string())
Output:
Name Age City Salary
0 Peter 23 Atlanta 55000
1 John 34 Denver 78000
2 Sara 29 Boston 92000
3 David 41 Seattle 66000
This will output the DataFrame with all columns, headers, and row labels.
We can also use to_string() with various other arguments. For example,
print(df.to_string(index=False, header=False))
Output:
Peter 23 Atlanta 55000
John 34 Denver 78000
Sara 29 Boston 92000
David 41 Seattle 66000
Here, index=False and header=False removes the row labels and column headers.
We can also use to_string() to format the values in the DataFrame. For instance,
df['Name'] = df['Name'].apply(lambda x: str(x).upper())
print(df.to_string(formatters={'Salary': '${:,.2f}'.format}))
Output:
Name Age City Salary
0 PETER 23 Atlanta $55,000.00
1 JOHN 34 Denver $78,000.00
2 SARA 29 Boston $92,000.00
3 DAVID 41 Seattle $66,000.00
This example shows how we can format the values in the 'Salary' column to include a dollar sign and commas.
Note: to_string() returns a string representation of the DataFrame. We can assign this string to a variable or write it to a file if needed.