简体   繁体   English

使用 pandas 从.csv 文件中读取垂直排列的数据

[英]Reading Vertically arranged data from .csv file using pandas

I have a.csv which looks like the image below:我有一个.csv,如下图所示:

表格表示

I want to create 4 data frames, which I am currently creating using.iloc我想创建 4 个数据框,我目前正在使用.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?有什么更 Pythonic 的方式来读取这些数据?

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这会生成一个数据帧列表,利用文件中数据补丁之间的空行将评估为 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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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