简体   繁体   中英

Reading Vertically arranged data from .csv file using pandas

I have a.csv which looks like the image below:

表格表示

I want to create 4 data frames, which I am currently creating using.iloc

import pandas as pd
enter code here
file_path='/file/path/name.csv'
df_main=pd.read_csv(file_path)
enter code here
df_global=df_main.iloc[:3,:]
df_mkt_a=df_main.iloc[6:9,:]
df_mkt_b=df_main.iloc[12:15,:]
df_mkt_c=df_main.iloc[18:21,:]

But this could run into problems on addition/deletion of row and is quite inflexible.

What can a more pythonic way to read such data?

This generates a list of dataframes, making use of the fact that the blank lines inbetween the datapatches in your file will evaluate to nan

df = pd.read_csv('test.csv', header =None)

# split at rows that have all nan entries
splits = [0] + [ix for ix in df[df.isnull().all(axis=1)].index] + [len(df)]
dfs = [df[splits[i]:splits[i+1]] for i in range(len(splits)-1)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM