Skip to content
Advertisement

Writting selective data values as float in csv/txt file using Pandas in Python

Using the internet information/information using this platform, I manage to write a small Python code which reads 200 csv and then able to distinguish the different values corresponding to the index of the column. Now I am interested in writting a csv/txt file in which 2 columns should there one of variable “time” and another variable “alpha.water” . Using the following Python script I am able to write a single variable “time”:

JavaScript

Also this “time” variable is written with the square bracket [] as follows:

JavaScript

Is there any clearer way of writing a csv/txt file where inside two columns of variable “Time” and “alpha.water”, the corresponding values be written? I am expecting the following output:

JavaScript

Any suggestion/comment will be a great help. Thanks in advance.

Advertisement

Answer

If you already have a dataframe created from a CSV and want to have another CSV, you can do this in two steps.

First, after loading the CSV into your dataframe drop the columns you don’t need aka. select the ones you do want. See this post for reference: https://stackoverflow.com/a/11287278/6826556

To combine different dataframes into one, use pandas concat functionality. Assuming you have one dataframe outside of the loop named df_all, you can concat the current one to that incrementally.

JavaScript

Then, you can export the new dataframe (that contains only the desired columns and is a combination of all others) to a new CSV file directly. Check out this link to see how.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement