Python Forum
Read CSV Files with multiple headers into Python DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read CSV Files with multiple headers into Python DataFrame
#9
my code from previous post works just fine with this sample csv file:

print '\n\nusing pandas with row 3 as column names\n'
import pandas as pd
DF = pd.read_csv('myfile.csv', skiprows=2) 
print DF.head(n=10) # print first 10 rows
Output:
using pandas with row 3 as column names  Timestamp (yyyy-mm-dd HH:MM:SS)    A  Bong    C    D    E      F    G    H  \ 0             2015-01-01 00:00:SS  734   734  734  734  734  734.0  734  734   1             2015-01-01 01:00:SS  816   816  816  816  816  816.0  816  816   2             2015-01-01 02:00:SS  114   114  114  114  114  114.0  114  114   3             2015-01-01 03:00:SS  822   822  822  822  822  822.0  822  822   4             2015-01-01 04:00:SS  569   569  569  569  569  569.0  569  569   5             2015-01-01 05:00:SS   87    87   87   87   87   87.0   87   87   6             2015-01-01 06:00:SS  250   250  250  250  250  250.0  250  250   7             2015-01-01 07:00:SS  492   492  492  492  492  492.0  492  492   8             2015-01-01 08:00:SS  862   862  862  862  862  862.0  862  862   9             2015-01-01 09:00:SS  869   869  869  869  869  869.0  869  869       I    J    K   0  734  734  734   1  816  816  816   2  114  114  114   3  822  822  822   4  569  569  569   5   87   87   87   6  250  250  250   7  492  492  492   8  862  862  862   9  869  869  869
also without column names:
print '\n\nusing pandas without column names\n'
import pandas as pd
DF = pd.read_csv('myfile.csv', skiprows=3, header=None) 
print DF.head(n=10) # print first 10 rows
Output:
using pandas without column names                     0    1    2    3    4    5      6    7    8    9    10  \ 0  2015-01-01 00:00:SS  734  734  734  734  734  734.0  734  734  734  734    1  2015-01-01 01:00:SS  816  816  816  816  816  816.0  816  816  816  816    2  2015-01-01 02:00:SS  114  114  114  114  114  114.0  114  114  114  114    3  2015-01-01 03:00:SS  822  822  822  822  822  822.0  822  822  822  822    4  2015-01-01 04:00:SS  569  569  569  569  569  569.0  569  569  569  569    5  2015-01-01 05:00:SS   87   87   87   87   87   87.0   87   87   87   87    6  2015-01-01 06:00:SS  250  250  250  250  250  250.0  250  250  250  250    7  2015-01-01 07:00:SS  492  492  492  492  492  492.0  492  492  492  492    8  2015-01-01 08:00:SS  862  862  862  862  862  862.0  862  862  862  862    9  2015-01-01 09:00:SS  869  869  869  869  869  869.0  869  869  869  869        11   0  734   1  816   2  114   3  822   4  569   5   87   6  250   7  492   8  862   9  869
Reply


Messages In This Thread
RE: Read CSV Files with multiple headers into Python DataFrame - by buran - Jan-24-2017, 07:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a Dataframe from Zenodo zip file with multiple CSVs about Spotify man0s 0 1,392 Apr-26-2022, 01:45 PM
Last Post: man0s
  Import multiple CSV files into pandas Krayna 0 1,762 May-20-2021, 04:56 PM
Last Post: Krayna
  Loading multiple JSON files to create a csv 0LI5A3A 0 2,149 Jun-28-2020, 10:35 PM
Last Post: 0LI5A3A
  Can python read Marathi text files and summarize them? mcp111 0 1,866 Mar-18-2020, 08:58 AM
Last Post: mcp111
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,531 Mar-14-2020, 09:19 AM
Last Post: scidam
  Append Multiple CSV files Nidhesh 2 2,559 Jul-03-2019, 11:55 AM
Last Post: Nidhesh
  How to extract different data groups from multiple CSV files using python Rafiz 3 3,332 Jun-04-2019, 05:20 PM
Last Post: jefsummers
  Sum product multiple Dataframes based on column headers. Lastwizzle 0 3,870 May-21-2019, 04:05 PM
Last Post: Lastwizzle
  Grab columns from multiple files, combine into one jon0852 0 2,062 Feb-12-2019, 02:53 AM
Last Post: jon0852
  Python QtableWidget get text of all cells and headers to dataframe Mady 3 23,376 Dec-15-2018, 06:46 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020