Python Forum
How get row index of matching row in column 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How get row index of matching row in column 0
#1
Hi,

I have a csv file as below:

inter value,70				
time interval[min],20				
dose_trigger value,-23				
					
					
warning_linit1,36				
warning limit2,15				
cooling time [hrs],2				
cooling number[#],30				
					
					
					
Trail_number,initila,final,middle,max,min
Trial_20157832,1,23,14,28,1
Standard,0,5,2.6,5,0
Trial_20037924,	2,13,17,20,0
Trial_20637924,6,18,19,17,3
I want to get the row index of "Trail_number" in column zero (here row8).

1. Extract the data from row zero to row7 (rows befor Trail_number row),and write to 'intial.csv'
2. Extract the data from row Trail_number(here row8) to end in to a 'final.csv'


I use the below code, but I am getting error in getteing row index.

import pandas as pd
import numpy as np
rw_data=pd.read_csv(r'D:\Mekala_Backupdata\PythonCodes\raw_WEHP02.csv')

idx1=~(rw_data.where(rw_data[0]=='Trail_number')).isnull().all()
error:

KeyError: 0
Reply
#2
(Apr-09-2019, 01:14 PM)Sri Wrote: I want to get the row index of "Trail_number" in column zero (here row8).

1. Extract the data from row zero to row7 (rows befor Trail_number row),and write to 'intial.csv'
2. Extract the data from row Trail_number(here row8) to end in to a 'final.csv'

I observe that row8 is cooling time [hrs],2 and 'Trail_number' is on row 13

Why use pandas, numpy if task is to write rows from one file into two separate files?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
There are 5 empty rows before "Trail_number"
if remove empty rows, the "Trail_number" will become row8.

I forgot to mention, I also want to delete empty rows.
I import pandas because I am reading CSV file using pd.read_csv.
Reply
#4
One way is to take advantage of iterator protocol. This is based on list, but file objects are also iterable and therefore might give some ideas.

s = ['inter value,70',
     'time interval[min],20',               
     'dose_trigger value,-23',              
     'warning_linit1,36',               
     'warning limit2,15',               
     'cooling time [hrs],2',                
     'cooling number[#],30',                
     'Trail_number,initila,final,middle,max,min',
     'Trial_20157832,1,23,14,28,1',
     'Standard,0,5,2.6,5,0',
     'Trial_20037924, 2,13,17,20,0',
     'Trial_20637924,6,18,19,17,3']

first = list()
second = list()

rows = iter(s)                          # create iterator

for row in rows:
    if row.startswith('Trail_number'):  # consume iterator until sentinel and break
        second.append(row)
        break
    else:
        first.append(row)

for row in rows:                         # consume remaining iterator
    second.append(row)

# first
['inter value,70',
 'time interval[min],20',
 'dose_trigger value,-23',
 'warning_linit1,36',
 'warning limit2,15',
 'cooling time [hrs],2',
 'cooling number[#],30']

# second
['Trail_number,initila,final,middle,max,min',
 'Trial_20157832,1,23,14,28,1',
 'Standard,0,5,2.6,5,0',
 'Trial_20037924, 2,13,17,20,0',
 'Trial_20637924,6,18,19,17,3']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
My original data is in csv and I read using

import pandas as pd
rw_data=pd.read_csv('raw_WEHP02.csv')

and now catch data before "Trail_number" in once csv and from "Trail_number" to end of the file write in another csv file.

as per your above suggestion I use the below code still does not work.

import pandas as pd
rw_data=pd.read_csv('aw_WEHP02.csv')

#=========================================

first = list()
second = list()
 
rows = iter(rw_data)                          # create iterator
 
for row in rows:
    if row.startswith('Trail_number'):  # consume iterator until sentinel and break
        second.append(row)
        break
    else:
        first.append(row)
 
for row in rows:                         # consume remaining iterator
    second.append(row)
Reply
#6
No need for pandas. Iterate over file object directly and instead of append write into appropriate file.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
No data catch in to first or second.
Reply
#8
As I explained, there is no need for pandas or any csv module. It's plain and simple reading from file and writing to file.

My example was with lists, but this principle works with files as well. However, implementing principle on files requires modification of code (no append but write).

with open('dummy_data.csv', 'r', encoding='UTF-8') as outf, \
     open('out_1.csv', 'w') as out_1, \
     open('out_2.csv', 'w') as out_2:
    
    for row in outf:
        if row.startswith('Trail_number'):
            out_2.write(row)
            break
        else:
            out_1.write(row)

    for row in outf:           # loop can be replace by: out_2.writelines(outf)
        out_2.write(row)
dummy_data.csv contained data provided and two new files contain required data:

Output:
$ cat out_1.csv inter value,70 time interval[min],20 dose_trigger value,-23 warning_linit1,36 warning limit2,15 cooling time [hrs],2 cooling number[#],30 $ cat out_2.csv Trail_number,initila,final,middle,max,min Trial_20157832,1,23,14,28,1 Standard,0,5,2.6,5,0 Trial_20037924, 2,13,17,20,0 Trial_20637924,6,18,19,17,3
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing the index column from df bnadir55 7 3,028 Apr-05-2022, 02:49 PM
Last Post: snippsat
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,297 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  Index error - columns vs non-column Vinny 3 4,913 Aug-09-2021, 04:46 PM
Last Post: snippsat
Question Matching variable to a list index Gilush 17 5,836 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  How to define index column name using df.to_csv SriRajesh 0 1,763 Feb-13-2020, 03:45 PM
Last Post: SriRajesh
  How to get row row index of matching row SriRajesh 0 1,249 Feb-13-2020, 02:35 PM
Last Post: SriRajesh
  How to ger matching rows data based on index columns SriRajesh 1 2,206 Mar-08-2019, 11:05 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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