How to Convert Dataframe to String: Various Approaches

Convert Dataframe to String

Pandas is the best python package for data manipulation. It has various function that allows you to perform all these manipulations. Do you want to convert dataframe to string? If yes then this post is for you. In this entire tutorial, you will learn how to convert entire pandas dataframe or particular column to a string.

Different Approaches to Convert Dataframe to String

Let’s create a sample dataframe that will be used for the conversion. Execute the below lines of code to create a sample dataframe.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df)

Output

Sample Dataframe for the conversion
Sample Dataframe for the conversion

Approach 1: Convert the entire dataframe to a string

Let’s start with the first approach. In it we will use the pandas.to_sring() function to convert the entire dataframe to string. You have to just pass the df to the to_string().

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df.to_string())
print(type(df.to_string()))

Output

entire dataframe to a string
entire dataframe to a string

Approach 2: Convert the dataframe to a string, without the index and column labels

Here I will pass the index=False, header=False to convert the dataframe without the index and column label.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df.to_string(index=False,header=False))
print(type(df.to_string()))

Output

 

to string, without the index and column labels
to string, without the index and column labels

Approach 3: Convert to a string for only one row

In this approach, you will pass the max_rows =1 to only convert the first row to a string.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df.to_string(max_rows=1))

Output

Convert to string for the max one number of row
Convert to string for the max one number of row

Approach 4: Convert to a string for only one column

Just like you did the conversion for one row in the same way you can do it for one column. You have to just use the max_cols= 1.

Execute the below lines of code.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df.to_string(max_cols=1))

Output

Convert to a string for only one column
Convert to a string for only one column

Approach 5: Converting to a string for columns only

You can also convert the df to string for specific columns only. For example, if I want to convert the df to string for the column name then I will pass the columns = [“name] inside the to_string() method.

Run the below lines of code.

import pandas as pd
data = {"name":["Sukesh","Abhishek","Maya","Rahul"],"age":[23,25,26,32],
"city":["Delhi","Noida","Las Vegas","Mumbai"]}
df = pd.DataFrame(data)
print(df.to_string(columns=["name"]))

Output

Converting to a string for columns only
Converting to a string for columns only

Conclusion

Pandas is the best library for data manipulation. In this entire tutorial, you have learned how to convert the entire data frame or particular column to a string. If you want to convert the data frame to string then these are approaches you can use to achieve that.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner