Home > Blockchain >  Saving columns from csv
Saving columns from csv

Time:10-07

I am trying to write a code that reads a csv file and can save each columns as a specific variable. I am having difficulty because the header is 7 lines long (something I can control but would like to just ignore if I can manipulate it in code), and then my data is full of important decimal places so it can not change to int( or maybe string?) I've also tried just saving each column by it's placement in the file but am struggling to run it. Any ideas? Image shows my current code that I have slimmed to show important parts and circles data that prints in my console.

CodePudding user response:

save each columns as a specific variable

import pandas as pd
pd.read_csv('file.csv')
x_col = df['X']

https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

CodePudding user response:

If what you are looking for is how to iterate through the columns, no matter how many there are. (Which is what I think you are asking.) Then this code should do the trick:

import pandas as pd
import csv
data = pd.read_csv('optitest.csv', skiprows=6)

for column in data.columns:
    # You will need to define what this save() method is.
    # Just placing it here as an example.
    save(data[column])

The line about formatting your data as a number or a string was a little vague. But if it's decimal data, then you need to use float. See #9637665.

  • Related