![](/img/trans.png)
[英]Extracting oddly arranged data from csv and converting to another csv file using python
[英]Reading Vertically arranged data from .csv file using pandas
我有一个.csv,如下图所示:
我想创建 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,:]
但这可能会在添加/删除行时遇到问题,并且非常不灵活。
有什么更 Pythonic 的方式来读取这些数据?
这会生成一个数据帧列表,利用文件中数据补丁之间的空行将评估为 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.