Skip to main content

Posts

Showing posts with the label write csv python

Comma separated values or csv: what is csv

 Introduction: Hi! today we are going to talk about a different and simple topic which I encountered recently in an interview. The topic is csv. As a data scientist, we use pandas daily to read csv files using pd.read_csv() functionality. And that is where the csv turns into a dataframe for us and we start data manipulation with the dataframe. But a csv is not a dataframe, and doesn't need pandas to be read. In this article, we will discuss a few ways to read a csv file, and will also take a deeper dive in how the csv and similar files are actually stored. What is csv? A csv or comma separated value file is a file where each data item is stored with comma separating one value from another. csv is the universally accepted format for tabular data. For a normal csv, each row is stored as a separate new line in the file; and each row contains each values separated by comma, but the row doesn't end with a comma.  How can we read a csv? A csv can be read using pandas ( read_csv...

How to write csv in python

Introduction: In day to day works of data analysts and scientists, you often need to write your results and present them in a nice and neat csv file or a excel file. In this article we will discuss how to write csv files from python scripts. It is important for not only data science but anyone who has to present data on a regular basis. The basic option: pandas.dataframe.to_csv attribute This option uses pandas library.If you are not introduced to pandas yet, read about pandas here first. When you have a data table in your environment which you want to save, then the best way is to put it in a dataframe version and then use the to_csv attribute of pandas dataframe to save it into a specific file. The normal use would look like: To clear the idea, the normal format to use to_csv on a dataframe is: dataframe.to_csv(destination_file_path_as_string,index = False) The above example depicts how you would save marks for 4 students. We first create a empty dataframe. Then we stor...