Writing Specific CSV Rows to a Dataframe
I am using the csv library to read specific rows from several files I have. The problem I am having is saving those rows into a dataframe. I am getting an indexing error that I can't solve.
The current version of the code finds the column names (which is on the third row) and then starts finding the data I need (which starts on the sixth row and continues until it hits a blank row). Finding the column names works fine, but when I try to append the data to it, I get the error:
"InvalidIndexError: Reindexing only valid with uniquely valued Index objects"
The code I currently have is as follows:
 i=0
 import csv
 import pandas as pd
 df = pd.DataFrame()
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: #this is for the column names
 print(row)
 df = pd.DataFrame(columns = row)
 df.columns = row
 if csvreader.line_num >= 6: #this is for the data
 if row: #checks for blank row
 if i<10: #just printing the top ten rows for debugging purposes, theres thousands I need
 print(i)
 i+=1
 df.append(row) #this is where I get the indexing error
 else: # breaks out of loop if
 break
 print(df) #for double checking if it worked
EDIT:
A sample of the data is here:
Devices
1680
Column Name 1,Column Name 2,Column Name 3,Column Name 4,Column Name 5,Column Name 6,Column Name 7,Column Name 8,Column Name 9,Column Name 10,Column Name 11,Column Name 12,Column Name 13,Column Name 14,Column Name 15,Column Name 16,Column Name 17,Column Name 18,Column Name 19,Column Name 20,Column Name 21
Frame,Sub Frame,Sync,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,FS,FS
,,,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V
1,0,0,1.28178e-005,-5.21866e-005,8.24e-006,1.19022e-005,1.00711e-005,3.02133e-005,2.83822e-005,0,6.40889e-006,-6.1037e-007,2.83822e-005,-6.40889e-006,2.65511e-005,1.46489e-005,1.73956e-005,1.09867e-005,0,0
1,1,0,9.82043e-006,-4.40121e-005,8.78497e-006,1.02673e-005,1.1706e-005,3.15758e-005,2.62023e-005,5.44972e-006,8.0438e-006,-1.06924e-005,2.91997e-005,-8.0438e-006,2.73686e-005,1.51939e-005,1.73956e-005,1.04417e-005,0,0
1,2,0,1.40167e-005,-3.27202e-005,1.00493e-005,1.22292e-005,1.33409e-005,3.55758e-005,2.57009e-005,6.58328e-006,9.67872e-006,-1.5499e-005,2.95376e-005,-8.47978e-006,2.98645e-005,1.47797e-005,1.42783e-005,9.89672e-006,0,0
1,3,0,1.83656e-005,-2.59735e-005,1.01692e-005,1.46816e-005,1.45617e-005,3.74506e-005,2.56355e-005,3.19357e-006,4.47972e-006,-1.95863e-005,2.93959e-005,-7.92392e-006,3.13469e-005,1.46489e-005,1.38423e-005,9.14466e-006,0,0
1,4,0,1.84419e-005,-2.20169e-005,8.5016e-006,1.52157e-005,1.46053e-005,3.87149e-005,2.44148e-005,6.53978e-007,-4.27252e-006,-1.96627e-005,2.87746e-005,-8.1528e-006,3.05185e-005,1.39513e-005,1.59568e-005,9.37354e-006,0,0
1,5,0,1.5837e-005,-1.80387e-005,7.46613e-006,1.39622e-005,1.40603e-005,4.07858e-005,2.10905e-005,0,-8.4253e-006,-1.45073e-005,2.88073e-005,-9.25364e-006,2.83277e-005,1.21529e-005,1.69705e-005,9.48254e-006,0,0
1,6,0,1.39295e-005,-1.44963e-005,7.52064e-006,1.24908e-005,1.42783e-005,4.23117e-005,1.63493e-005,0,-4.77405e-006,-9.22096e-006,2.98427e-005,-1.00711e-005,2.60933e-005,1.02455e-005,1.5935e-005,7.84765e-006,0,0
I want the output to be a dataframe with row 3 as the column names and row 6 until a blank row as the data filling the columns.
For example:
 In[1]: csv file above
 Out[1]: [column Name 1] [Column Name 2] ...
 [Data 1 in Row 6] [Data 2 in Row 6] ...
 [Data 1 in Row 7] [Data 2 in Row 7] ...
 [Data 1 in Row 8] [Data 2 in Row 8] ...
python pandas csv dataframe
|
show 1 more comment
I am using the csv library to read specific rows from several files I have. The problem I am having is saving those rows into a dataframe. I am getting an indexing error that I can't solve.
The current version of the code finds the column names (which is on the third row) and then starts finding the data I need (which starts on the sixth row and continues until it hits a blank row). Finding the column names works fine, but when I try to append the data to it, I get the error:
"InvalidIndexError: Reindexing only valid with uniquely valued Index objects"
The code I currently have is as follows:
 i=0
 import csv
 import pandas as pd
 df = pd.DataFrame()
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: #this is for the column names
 print(row)
 df = pd.DataFrame(columns = row)
 df.columns = row
 if csvreader.line_num >= 6: #this is for the data
 if row: #checks for blank row
 if i<10: #just printing the top ten rows for debugging purposes, theres thousands I need
 print(i)
 i+=1
 df.append(row) #this is where I get the indexing error
 else: # breaks out of loop if
 break
 print(df) #for double checking if it worked
EDIT:
A sample of the data is here:
Devices
1680
Column Name 1,Column Name 2,Column Name 3,Column Name 4,Column Name 5,Column Name 6,Column Name 7,Column Name 8,Column Name 9,Column Name 10,Column Name 11,Column Name 12,Column Name 13,Column Name 14,Column Name 15,Column Name 16,Column Name 17,Column Name 18,Column Name 19,Column Name 20,Column Name 21
Frame,Sub Frame,Sync,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,FS,FS
,,,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V
1,0,0,1.28178e-005,-5.21866e-005,8.24e-006,1.19022e-005,1.00711e-005,3.02133e-005,2.83822e-005,0,6.40889e-006,-6.1037e-007,2.83822e-005,-6.40889e-006,2.65511e-005,1.46489e-005,1.73956e-005,1.09867e-005,0,0
1,1,0,9.82043e-006,-4.40121e-005,8.78497e-006,1.02673e-005,1.1706e-005,3.15758e-005,2.62023e-005,5.44972e-006,8.0438e-006,-1.06924e-005,2.91997e-005,-8.0438e-006,2.73686e-005,1.51939e-005,1.73956e-005,1.04417e-005,0,0
1,2,0,1.40167e-005,-3.27202e-005,1.00493e-005,1.22292e-005,1.33409e-005,3.55758e-005,2.57009e-005,6.58328e-006,9.67872e-006,-1.5499e-005,2.95376e-005,-8.47978e-006,2.98645e-005,1.47797e-005,1.42783e-005,9.89672e-006,0,0
1,3,0,1.83656e-005,-2.59735e-005,1.01692e-005,1.46816e-005,1.45617e-005,3.74506e-005,2.56355e-005,3.19357e-006,4.47972e-006,-1.95863e-005,2.93959e-005,-7.92392e-006,3.13469e-005,1.46489e-005,1.38423e-005,9.14466e-006,0,0
1,4,0,1.84419e-005,-2.20169e-005,8.5016e-006,1.52157e-005,1.46053e-005,3.87149e-005,2.44148e-005,6.53978e-007,-4.27252e-006,-1.96627e-005,2.87746e-005,-8.1528e-006,3.05185e-005,1.39513e-005,1.59568e-005,9.37354e-006,0,0
1,5,0,1.5837e-005,-1.80387e-005,7.46613e-006,1.39622e-005,1.40603e-005,4.07858e-005,2.10905e-005,0,-8.4253e-006,-1.45073e-005,2.88073e-005,-9.25364e-006,2.83277e-005,1.21529e-005,1.69705e-005,9.48254e-006,0,0
1,6,0,1.39295e-005,-1.44963e-005,7.52064e-006,1.24908e-005,1.42783e-005,4.23117e-005,1.63493e-005,0,-4.77405e-006,-9.22096e-006,2.98427e-005,-1.00711e-005,2.60933e-005,1.02455e-005,1.5935e-005,7.84765e-006,0,0
I want the output to be a dataframe with row 3 as the column names and row 6 until a blank row as the data filling the columns.
For example:
 In[1]: csv file above
 Out[1]: [column Name 1] [Column Name 2] ...
 [Data 1 in Row 6] [Data 2 in Row 6] ...
 [Data 1 in Row 7] [Data 2 in Row 7] ...
 [Data 1 in Row 8] [Data 2 in Row 8] ...
python pandas csv dataframe
 
 
 
 
 
 
 
 Can you add some data sample and expected output?
 
 – jezrael
 Nov 13 '18 at 6:37
 
 
 
 
 
 
 
 
 
 
 Yes, I have added it
 
 – TinyMuffin
 Nov 13 '18 at 6:42
 
 
 
 
 
 
 
 
 
 
 I think sample data in text, if use pictures I cannot copy data. Also what is expected output?
 
 – jezrael
 Nov 13 '18 at 6:46
 
 
 
 
 
 
 
 
 
 
 I added text. Expected output was below the sample.
 
 – TinyMuffin
 Nov 13 '18 at 7:08
 
 
 
 
 
 
 
 
 
 
 Sorry, can you check how to provide a great pandas example ?
 
 – jezrael
 Nov 13 '18 at 7:10
 
 
 
|
show 1 more comment
I am using the csv library to read specific rows from several files I have. The problem I am having is saving those rows into a dataframe. I am getting an indexing error that I can't solve.
The current version of the code finds the column names (which is on the third row) and then starts finding the data I need (which starts on the sixth row and continues until it hits a blank row). Finding the column names works fine, but when I try to append the data to it, I get the error:
"InvalidIndexError: Reindexing only valid with uniquely valued Index objects"
The code I currently have is as follows:
 i=0
 import csv
 import pandas as pd
 df = pd.DataFrame()
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: #this is for the column names
 print(row)
 df = pd.DataFrame(columns = row)
 df.columns = row
 if csvreader.line_num >= 6: #this is for the data
 if row: #checks for blank row
 if i<10: #just printing the top ten rows for debugging purposes, theres thousands I need
 print(i)
 i+=1
 df.append(row) #this is where I get the indexing error
 else: # breaks out of loop if
 break
 print(df) #for double checking if it worked
EDIT:
A sample of the data is here:
Devices
1680
Column Name 1,Column Name 2,Column Name 3,Column Name 4,Column Name 5,Column Name 6,Column Name 7,Column Name 8,Column Name 9,Column Name 10,Column Name 11,Column Name 12,Column Name 13,Column Name 14,Column Name 15,Column Name 16,Column Name 17,Column Name 18,Column Name 19,Column Name 20,Column Name 21
Frame,Sub Frame,Sync,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,FS,FS
,,,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V
1,0,0,1.28178e-005,-5.21866e-005,8.24e-006,1.19022e-005,1.00711e-005,3.02133e-005,2.83822e-005,0,6.40889e-006,-6.1037e-007,2.83822e-005,-6.40889e-006,2.65511e-005,1.46489e-005,1.73956e-005,1.09867e-005,0,0
1,1,0,9.82043e-006,-4.40121e-005,8.78497e-006,1.02673e-005,1.1706e-005,3.15758e-005,2.62023e-005,5.44972e-006,8.0438e-006,-1.06924e-005,2.91997e-005,-8.0438e-006,2.73686e-005,1.51939e-005,1.73956e-005,1.04417e-005,0,0
1,2,0,1.40167e-005,-3.27202e-005,1.00493e-005,1.22292e-005,1.33409e-005,3.55758e-005,2.57009e-005,6.58328e-006,9.67872e-006,-1.5499e-005,2.95376e-005,-8.47978e-006,2.98645e-005,1.47797e-005,1.42783e-005,9.89672e-006,0,0
1,3,0,1.83656e-005,-2.59735e-005,1.01692e-005,1.46816e-005,1.45617e-005,3.74506e-005,2.56355e-005,3.19357e-006,4.47972e-006,-1.95863e-005,2.93959e-005,-7.92392e-006,3.13469e-005,1.46489e-005,1.38423e-005,9.14466e-006,0,0
1,4,0,1.84419e-005,-2.20169e-005,8.5016e-006,1.52157e-005,1.46053e-005,3.87149e-005,2.44148e-005,6.53978e-007,-4.27252e-006,-1.96627e-005,2.87746e-005,-8.1528e-006,3.05185e-005,1.39513e-005,1.59568e-005,9.37354e-006,0,0
1,5,0,1.5837e-005,-1.80387e-005,7.46613e-006,1.39622e-005,1.40603e-005,4.07858e-005,2.10905e-005,0,-8.4253e-006,-1.45073e-005,2.88073e-005,-9.25364e-006,2.83277e-005,1.21529e-005,1.69705e-005,9.48254e-006,0,0
1,6,0,1.39295e-005,-1.44963e-005,7.52064e-006,1.24908e-005,1.42783e-005,4.23117e-005,1.63493e-005,0,-4.77405e-006,-9.22096e-006,2.98427e-005,-1.00711e-005,2.60933e-005,1.02455e-005,1.5935e-005,7.84765e-006,0,0
I want the output to be a dataframe with row 3 as the column names and row 6 until a blank row as the data filling the columns.
For example:
 In[1]: csv file above
 Out[1]: [column Name 1] [Column Name 2] ...
 [Data 1 in Row 6] [Data 2 in Row 6] ...
 [Data 1 in Row 7] [Data 2 in Row 7] ...
 [Data 1 in Row 8] [Data 2 in Row 8] ...
python pandas csv dataframe
I am using the csv library to read specific rows from several files I have. The problem I am having is saving those rows into a dataframe. I am getting an indexing error that I can't solve.
The current version of the code finds the column names (which is on the third row) and then starts finding the data I need (which starts on the sixth row and continues until it hits a blank row). Finding the column names works fine, but when I try to append the data to it, I get the error:
"InvalidIndexError: Reindexing only valid with uniquely valued Index objects"
The code I currently have is as follows:
 i=0
 import csv
 import pandas as pd
 df = pd.DataFrame()
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: #this is for the column names
 print(row)
 df = pd.DataFrame(columns = row)
 df.columns = row
 if csvreader.line_num >= 6: #this is for the data
 if row: #checks for blank row
 if i<10: #just printing the top ten rows for debugging purposes, theres thousands I need
 print(i)
 i+=1
 df.append(row) #this is where I get the indexing error
 else: # breaks out of loop if
 break
 print(df) #for double checking if it worked
EDIT:
A sample of the data is here:
Devices
1680
Column Name 1,Column Name 2,Column Name 3,Column Name 4,Column Name 5,Column Name 6,Column Name 7,Column Name 8,Column Name 9,Column Name 10,Column Name 11,Column Name 12,Column Name 13,Column Name 14,Column Name 15,Column Name 16,Column Name 17,Column Name 18,Column Name 19,Column Name 20,Column Name 21
Frame,Sub Frame,Sync,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,v,FS,FS
,,,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V,V
1,0,0,1.28178e-005,-5.21866e-005,8.24e-006,1.19022e-005,1.00711e-005,3.02133e-005,2.83822e-005,0,6.40889e-006,-6.1037e-007,2.83822e-005,-6.40889e-006,2.65511e-005,1.46489e-005,1.73956e-005,1.09867e-005,0,0
1,1,0,9.82043e-006,-4.40121e-005,8.78497e-006,1.02673e-005,1.1706e-005,3.15758e-005,2.62023e-005,5.44972e-006,8.0438e-006,-1.06924e-005,2.91997e-005,-8.0438e-006,2.73686e-005,1.51939e-005,1.73956e-005,1.04417e-005,0,0
1,2,0,1.40167e-005,-3.27202e-005,1.00493e-005,1.22292e-005,1.33409e-005,3.55758e-005,2.57009e-005,6.58328e-006,9.67872e-006,-1.5499e-005,2.95376e-005,-8.47978e-006,2.98645e-005,1.47797e-005,1.42783e-005,9.89672e-006,0,0
1,3,0,1.83656e-005,-2.59735e-005,1.01692e-005,1.46816e-005,1.45617e-005,3.74506e-005,2.56355e-005,3.19357e-006,4.47972e-006,-1.95863e-005,2.93959e-005,-7.92392e-006,3.13469e-005,1.46489e-005,1.38423e-005,9.14466e-006,0,0
1,4,0,1.84419e-005,-2.20169e-005,8.5016e-006,1.52157e-005,1.46053e-005,3.87149e-005,2.44148e-005,6.53978e-007,-4.27252e-006,-1.96627e-005,2.87746e-005,-8.1528e-006,3.05185e-005,1.39513e-005,1.59568e-005,9.37354e-006,0,0
1,5,0,1.5837e-005,-1.80387e-005,7.46613e-006,1.39622e-005,1.40603e-005,4.07858e-005,2.10905e-005,0,-8.4253e-006,-1.45073e-005,2.88073e-005,-9.25364e-006,2.83277e-005,1.21529e-005,1.69705e-005,9.48254e-006,0,0
1,6,0,1.39295e-005,-1.44963e-005,7.52064e-006,1.24908e-005,1.42783e-005,4.23117e-005,1.63493e-005,0,-4.77405e-006,-9.22096e-006,2.98427e-005,-1.00711e-005,2.60933e-005,1.02455e-005,1.5935e-005,7.84765e-006,0,0
I want the output to be a dataframe with row 3 as the column names and row 6 until a blank row as the data filling the columns.
For example:
 In[1]: csv file above
 Out[1]: [column Name 1] [Column Name 2] ...
 [Data 1 in Row 6] [Data 2 in Row 6] ...
 [Data 1 in Row 7] [Data 2 in Row 7] ...
 [Data 1 in Row 8] [Data 2 in Row 8] ...
python pandas csv dataframe
python pandas csv dataframe
edited Nov 13 '18 at 8:11
Anton vBR
11.6k21124
11.6k21124
asked Nov 13 '18 at 6:36
TinyMuffinTinyMuffin
286
286
 
 
 
 
 
 
 
 Can you add some data sample and expected output?
 
 – jezrael
 Nov 13 '18 at 6:37
 
 
 
 
 
 
 
 
 
 
 Yes, I have added it
 
 – TinyMuffin
 Nov 13 '18 at 6:42
 
 
 
 
 
 
 
 
 
 
 I think sample data in text, if use pictures I cannot copy data. Also what is expected output?
 
 – jezrael
 Nov 13 '18 at 6:46
 
 
 
 
 
 
 
 
 
 
 I added text. Expected output was below the sample.
 
 – TinyMuffin
 Nov 13 '18 at 7:08
 
 
 
 
 
 
 
 
 
 
 Sorry, can you check how to provide a great pandas example ?
 
 – jezrael
 Nov 13 '18 at 7:10
 
 
 
|
show 1 more comment
 
 
 
 
 
 
 
 Can you add some data sample and expected output?
 
 – jezrael
 Nov 13 '18 at 6:37
 
 
 
 
 
 
 
 
 
 
 Yes, I have added it
 
 – TinyMuffin
 Nov 13 '18 at 6:42
 
 
 
 
 
 
 
 
 
 
 I think sample data in text, if use pictures I cannot copy data. Also what is expected output?
 
 – jezrael
 Nov 13 '18 at 6:46
 
 
 
 
 
 
 
 
 
 
 I added text. Expected output was below the sample.
 
 – TinyMuffin
 Nov 13 '18 at 7:08
 
 
 
 
 
 
 
 
 
 
 Sorry, can you check how to provide a great pandas example ?
 
 – jezrael
 Nov 13 '18 at 7:10
 
 
 
Can you add some data sample and expected output?
– jezrael
Nov 13 '18 at 6:37
Can you add some data sample and expected output?
– jezrael
Nov 13 '18 at 6:37
Yes, I have added it
– TinyMuffin
Nov 13 '18 at 6:42
Yes, I have added it
– TinyMuffin
Nov 13 '18 at 6:42
I think sample data in text, if use pictures I cannot copy data. Also what is expected output?
– jezrael
Nov 13 '18 at 6:46
I think sample data in text, if use pictures I cannot copy data. Also what is expected output?
– jezrael
Nov 13 '18 at 6:46
I added text. Expected output was below the sample.
– TinyMuffin
Nov 13 '18 at 7:08
I added text. Expected output was below the sample.
– TinyMuffin
Nov 13 '18 at 7:08
Sorry, can you check how to provide a great pandas example ?
– jezrael
Nov 13 '18 at 7:10
Sorry, can you check how to provide a great pandas example ?
– jezrael
Nov 13 '18 at 7:10
|
show 1 more comment
 1 Answer
 1
 
active
oldest
votes
I appreciate being downvoted without given reasons why my question deserves a downvote. I was able to figure it out on my own though. Hopefully, this may answer other people's questions in the future.
 import csv
 import pandas as pd
 temp = #initialize array
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: 
 temp.append(row) #gets column names and saves to array 
 if csvreader.line_num >= 6:
 if row: 
 temp.append(row) # gets data values and saves to array
 else: #stops at blank row
 break
 df = pd.DataFrame(temp) #creates a dataframe from an array
 df.columns = df.iloc[0] #make top row the column names
 df.reindex(df.index.drop(1))
 print(df)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275142%2fwriting-specific-csv-rows-to-a-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
 1 Answer
 1
 
active
oldest
votes
 1 Answer
 1
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
I appreciate being downvoted without given reasons why my question deserves a downvote. I was able to figure it out on my own though. Hopefully, this may answer other people's questions in the future.
 import csv
 import pandas as pd
 temp = #initialize array
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: 
 temp.append(row) #gets column names and saves to array 
 if csvreader.line_num >= 6:
 if row: 
 temp.append(row) # gets data values and saves to array
 else: #stops at blank row
 break
 df = pd.DataFrame(temp) #creates a dataframe from an array
 df.columns = df.iloc[0] #make top row the column names
 df.reindex(df.index.drop(1))
 print(df)
add a comment |
I appreciate being downvoted without given reasons why my question deserves a downvote. I was able to figure it out on my own though. Hopefully, this may answer other people's questions in the future.
 import csv
 import pandas as pd
 temp = #initialize array
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: 
 temp.append(row) #gets column names and saves to array 
 if csvreader.line_num >= 6:
 if row: 
 temp.append(row) # gets data values and saves to array
 else: #stops at blank row
 break
 df = pd.DataFrame(temp) #creates a dataframe from an array
 df.columns = df.iloc[0] #make top row the column names
 df.reindex(df.index.drop(1))
 print(df)
add a comment |
I appreciate being downvoted without given reasons why my question deserves a downvote. I was able to figure it out on my own though. Hopefully, this may answer other people's questions in the future.
 import csv
 import pandas as pd
 temp = #initialize array
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: 
 temp.append(row) #gets column names and saves to array 
 if csvreader.line_num >= 6:
 if row: 
 temp.append(row) # gets data values and saves to array
 else: #stops at blank row
 break
 df = pd.DataFrame(temp) #creates a dataframe from an array
 df.columns = df.iloc[0] #make top row the column names
 df.reindex(df.index.drop(1))
 print(df)
I appreciate being downvoted without given reasons why my question deserves a downvote. I was able to figure it out on my own though. Hopefully, this may answer other people's questions in the future.
 import csv
 import pandas as pd
 temp = #initialize array
 with open('C:/Users/sword/Anaconda3/envs/exceltest/RF_SubjP02_Free_STATIC_TR01.csv', 'r') as csvfile:
 csvreader = csv.reader(csvfile, delimiter=',')
 for row in csvreader:
 if csvreader.line_num == 3: 
 temp.append(row) #gets column names and saves to array 
 if csvreader.line_num >= 6:
 if row: 
 temp.append(row) # gets data values and saves to array
 else: #stops at blank row
 break
 df = pd.DataFrame(temp) #creates a dataframe from an array
 df.columns = df.iloc[0] #make top row the column names
 df.reindex(df.index.drop(1))
 print(df)
answered Nov 15 '18 at 21:59
TinyMuffinTinyMuffin
286
286
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275142%2fwriting-specific-csv-rows-to-a-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
 
Can you add some data sample and expected output?
– jezrael
Nov 13 '18 at 6:37
Yes, I have added it
– TinyMuffin
Nov 13 '18 at 6:42
I think sample data in text, if use pictures I cannot copy data. Also what is expected output?
– jezrael
Nov 13 '18 at 6:46
I added text. Expected output was below the sample.
– TinyMuffin
Nov 13 '18 at 7:08
Sorry, can you check how to provide a great pandas example ?
– jezrael
Nov 13 '18 at 7:10