{"id":26338,"date":"2021-12-17T08:43:04","date_gmt":"2021-12-17T03:13:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=26338"},"modified":"2021-12-17T08:43:04","modified_gmt":"2021-12-17T03:13:04","slug":"in-python-how-do-you-save-a-dataframe-as-a-csv-file","status":"publish","type":"post","link":"https:\/\/python-programs.com\/in-python-how-do-you-save-a-dataframe-as-a-csv-file\/","title":{"rendered":"In Python, How Do you Save a Dataframe as a csv File?"},"content":{"rendered":"
Pandas Module in Python:<\/strong><\/p>\n Pandas is an open-source library based on the NumPy library. It enables users to perform effective data analysis, cleaning, and preparation. Pandas is fast, and it provides users with high performance and productivity.<\/p>\n The majority of the datasets you work with are referred to as DataFrames. DataFrames is a 2-Dimensional labeled Data Structure containing indexes for rows and columns, and each cell can store any form of value. DataFrames are simply Dictionary-based NumPy Arrays.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We frequently encounter circumstances in which we need to save large amounts of data generated by scraping or analysis in an easy, accesible\u00a0\u00a0and readable form\u00a0rather shareable form.<\/p>\n We can achieve this now by storing the data frame as a csv<\/strong> file.<\/p>\n We can save a data frame as a CSV file using the pandas.to_csv()<\/strong> function. The file name must be passed as a parameter to the method.<\/p>\n Example<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Pandas Module in Python: Pandas is an open-source library based on the NumPy library. It enables users to perform effective data analysis, cleaning, and preparation. Pandas is fast, and it provides users with high performance and productivity. The majority of the datasets you work with are referred to as DataFrames. DataFrames is a 2-Dimensional labeled …<\/p>\nCreating a Dataframe<\/h4>\n
\n
# Import os module using the import keyword.\r\nimport os\r\n# Import pandas module using the import keyword.\r\nimport pandas\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\", \"Kiwi\"]\r\n# Create a dictionary for the above fruits list and store it in another variable.\r\nfruit_dictnry = {'Types of Fruits': fruit_lst}\r\n# Pass the above fruit_dictnry as an argument to the pandas.DataFrame() function\r\n# to create a dataframe for the above dictionary.\r\nrslt_dataframe = pandas.DataFrame(fruit_dictnry)\r\n# Print the above obtained dataframe.\r\nprint(rslt_dataframe)<\/pre>\n
Types of Fruits\r\n0 Apple\r\n1 mango\r\n2 banana\r\n3 orange\r\n4 grapes\r\n5 Kiwi<\/pre>\n
How to Save this Dataframe?<\/h4>\n
dataframe.to_csv('filename.csv')<\/pre>\n
\n
from pandas.core.frame import DataFrame\r\n# Import os module using the import keyword.\r\nimport os\r\n# Import pandas module using the import keyword.\r\nimport pandas\r\n# Give some random list(fruits) as static input and store it in a variable.\r\nfruit_lst = [\"Apple\", \"mango\", \"banana\", \"orange\", \"grapes\", \"Kiwi\"]\r\n# Create a dictionary for the above fruits list and store it in another variable.\r\nfruit_dictnry = {'Types of Fruits': fruit_lst}\r\n# Pass the above fruit_dictnry as an argument to the pandas.DataFrame() function\r\n# to create a dataframe for the above dictionary.\r\nrslt_dataframe = pandas.DataFrame(fruit_dictnry)\r\n# Save the above dataframe as a CSV file using the to_csv() function by passing \r\n# some random file name as an argument that you want to save with.\r\nrslt_dataframe.to_csv('fruits.csv')<\/pre>\n
\n\n
\n \n<\/th>\n Types of Fruits<\/th>\n<\/tr>\n<\/thead>\n \n 0<\/td>\n Apple<\/td>\n<\/tr>\n \n 1<\/td>\n mango<\/td>\n<\/tr>\n \n 2<\/td>\n banana<\/td>\n<\/tr>\n \n 3<\/td>\n orange<\/td>\n<\/tr>\n \n 4<\/td>\n grapes<\/td>\n<\/tr>\n \n 5<\/td>\n Kiwi<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n