开发者社区> 问答> 正文

dataframe中如何从csv文件中每隔n行来创建dataframe?

dataframe中如何从csv文件中每隔n行来创建dataframe?

展开
收起
游客y244y7ln2rlpa 2021-12-05 20:05:22 428 0
1 条回答
写回答
取消 提交回答
  • # 每隔50行读取一行数据
    df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv', chunksize=50)
    df2 = pd.DataFrame()
    for chunk in df:
        # 获取series
        df2 = df2.append(chunk.iloc[0,:])
    
    #显示前5行
    print(df2.head())
    
    #>	                  crim    zn  indus chas                  nox     rm   age  \
    	0              0.21977   0.0   6.91    0  0.44799999999999995  5.602  62.0   
    	1               0.0686   0.0   2.89    0                0.445  7.416  62.5   
    	2   2.7339700000000002   0.0  19.58    0                0.871  5.597  94.9   
    	3               0.0315  95.0   1.47    0  0.40299999999999997  6.975  15.3   
    	4  0.19072999999999998  22.0   5.86    0                0.431  6.718  17.5   
    	
    	      dis rad  tax ptratio       b  lstat  medv  
    	0  6.0877   3  233    17.9   396.9   16.2  19.4  
    	1  3.4952   2  276    18.0   396.9   6.19  33.2  
    	2  1.5257   5  403    14.7  351.85  21.45  15.4  
    	3  7.6534   3  402    17.0   396.9   4.56  34.9  
    	4  7.8265   7  330    19.1  393.74   6.56  26.2  
    
    
    2021-12-05 22:22:37
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
demystifying dataframe and dataset 立即下载
Data Wrangling with PySpark for Data Scientists Who Know Pandas 立即下载
Adopting Dataframes and Parque 立即下载