Sep-06-2023, 09:14 AM
This is a neat way of doing it. the difficulty is that I am importing the data directly from my machine through GPIB and its importing as a str "type" and I am not actually calling a CSV file. So Im stuck at being able to seperate the string into seperate columns. The screen shot of the data I show is how the Python imports the data directly from the machine.
(Sep-05-2023, 06:45 PM)deanhystad Wrote: Please post text, not screenshots. When you post text, others can easily copy/paste to create data for their own testing.
It looks like this would be easy using pandas.
I mocked up some data that looks something like yours. Tab delimited values with some lines at the top that need to be skipped, and an annoying "!" before the column headers.
This code reads the file to extract the column names, then uses pandas to read the file into a dataframe.
Output:# 34589043! FILE NAME ! DATE NOW ! RAW DATA # More information ! FREQ S11M S21M S21A 0.11444405920970335 0.22851792934978143 0.16724464908750036 0.054813079444560264 0.11565032195951042 0.23010933442970138 0.3048773751796404 0.11465260170716174 0.20102541854079847 0.27830101316888567 0.3995954975408248 0.12034618280172404 0.41276142607996513 0.31147127777115224 0.46636510024384636 0.2806783250819618 0.495356229079312 0.3495931528852747 0.6389401268977468 0.38079631999232333 0.556074773658631 0.3894689120388013 0.6779475794713076 0.43728550829612134 0.6332585933784733 0.4924761223011904 0.7503465368809027 0.7386632480838207 0.6341059239183447 0.5130383410762516 0.8554371206142789 0.7695854491274398 0.8902822004610119 0.7219019187548961 0.8712110079483135 0.8443884202719537 0.9170577193238573 0.7970927214052331 0.9344766589022325 0.8597198838156557
import pandas as pd # Read column names from 4th line in file. Remove leading "!" with open("data.csv", "r") as file: columns = file.readlines()[4][1:].split() # Read file into dataframe. Skip the first 5 rows and use the column # names from above. df = pd.read_csv("data.csv", sep="\t", skiprows=5, names=columns) print(df)Now it is easy to get the columns.
Output:FREQ S11M S21M S21A 0 0.114444 0.228518 0.167245 0.054813 1 0.115650 0.230109 0.304877 0.114653 2 0.201025 0.278301 0.399595 0.120346 3 0.412761 0.311471 0.466365 0.280678 4 0.495356 0.349593 0.638940 0.380796 5 0.556075 0.389469 0.677948 0.437286 6 0.633259 0.492476 0.750347 0.738663 7 0.634106 0.513038 0.855437 0.769585 8 0.890282 0.721902 0.871211 0.844388 9 0.917058 0.797093 0.934477 0.859720
print(df["FREQ"])
Output:0 0.114444 1 0.115650 2 0.201025 3 0.412761 4 0.495356 5 0.556075 6 0.633259 7 0.634106 8 0.890282 9 0.917058 Name: FREQ, dtype: float64