'How to extract/read specific information from a data frame, to make plots
It has been a while. I have figured out several different ways of reading in csv/TXT/xls files and done rudimentary data plots. Now, I have a two-fold question. My first question is (based on a conversation I had with coworkers) is this the best way to read the data into a frame, or should it be done in a different manner? I have thousands of CSV files from a project, with 5 columns of data I am interested in.
These are now read into a df and listed in a way that I like. As a career software user, I'm at a disadvantage now. Normally in a spreadsheet, I would select, then plot the ones I want, but that's not why we use python.
Each CSV starts with the CSV file name, then colon, then the first column. I would like to selectively fetch data based on file name (this is what I haven't figured out), and plot x,y based on column names (I have this figured out for a single CSV file).
Next question is how can this be done (extract selectively for plotting)? Each file has 93 rows, and there are many in a single frame. So take "this" sample and "that" sample for x,y plot comparison), or should I read in the data another way, such as separate dataframes (this was also suggested)?
This would be extracting one set of 93 rows selectively from amongst the many present in a dataframe.
#Created on Thu Feb 17 10:04:22 2022
import pandas as pd
import os
from matplotlib import pyplot as plt
import numpy as np
path=r"C:path" # sets file path
os.chdir(path) #this tells python to change working directory to file path
path = os.getcwd()
cols=[4,10,11,12,13] # use only these columns (may have to cheat and look in excel to find the ones you want)
csvs = [x for x in os.listdir('.') if x.endswith('.csv')]
# stats.csv -> stats
fns = [os.path.splitext(os.path.basename(x))[0] for x in csvs]
d = {}
for i in range(len(fns)):
d[fns[i]] = pd.read_csv(csvs[i],low_memory=True, usecols=cols, header=2)
#the next part is for creating plots and subplots, and goes beyond the curent question I believe```
[1]: https://i.stack.imgur.com/dACP6.jpg
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
