Loading Multiple Tables in a Single csv File

Asked 2 years ago, Updated 2 years ago, 37 views

This is the RorPython environment for Win10.

Does anyone know a simple way to load a single csv file with more than one table?Do you have any packages of R?

that 1,x1,q2,q4,x9
2, aa, 3, 4, 0
2, you, 2, 3, 1

2, x1, q2, q4, x9
5, aa, 3, 7, 0
2, you, 4, 3, 1
7, express, 1, 1, 2
0, no 1, 3, 6, 5

its 6, x1, q2, q4, x9
5,xyz,3,2,0

This is a pattern with the same column name but multiple tables in a single csv file with one row open.
(Please do not copy manually with Excel or editor…
) There are a lot of files and tables.)

Thank you for your cooperation.

****Additional ****

It seems that each table has a different column name only at the beginning...
R's loading doesn't seem to have any effect...
Sorry.

python r

2022-09-29 22:28

2 Answers

The following is an example of using R (external packages are not used):
The data file name is tables.csv, and the result is a list of data frames.

>fname<-'tables.csv'
>tbls<-unlist(strsplit(readChar(fname, file.info(fname)$size),
                          split='(\r?\n){2}', perl=T))
>dfs<-lapply(tbls, function(t){read.csv(text=t)})
> length (dfs)
[1] 3
>dfs[[1]]
  q1 x1 q2 q4 x9
12 aa 340
Two, you, two, three, one.
>dfs[[2]]
  q1 x1 q2 q4 x9
15 aaa370
22 you 431
37 express 1 1 2
40 no 13 65
>dfs[[3]]
  q1 x1 q2 q4 x9
15xyz3 20


2022-09-29 22:28

The answer depends on how you use multiple tables, but if you want to ignore the middle blank line and header industry, Python can use Pandas to select columns with only numbers and no blanks and process them with the following code:

import pandas as pd
import numpy as np

column = 0
df = pd.read_csv('test2.csv', converters = {column:lambdax:x if x.ismeric()elsenp.nan})
df.dropna(subset=[df.columns[column]], replace=True)

# if you want to save
df.to_csv(save_fname, index=false)


2022-09-29 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.