简体   繁体   中英

Save skip rows in pandas read csv

I have a list of skip rows ( say [1,5,10] --> row numbers) and when I passed this to pandas read_csv , it ignores those rows. But, I need to save these skipped rows in a different text file.

I went through pandas read_csv documentation and few other articles, but have no idea how to save this into a text file.

Example :

Input file :

a,b,c
# Some Junk to Skip 1
4,5,6
# Some junk to skip 2
9,20,9
2,3,4
5,6,7

Code :

skiprows = [1,3]
df = pandas.read_csv(file, skip_rows = skiprows)

Now output.txt :

# Some junk to skip 1
# Some junk to skip 2

Thanks in advance!

def write_skiprows(infile, skiprows, outfile='skiprows.csv')
    maxrow = max(skiprows)
    with open(infile, 'r') as f, open(outfile, 'w') as o:
        for i, line in enumerate(f):
            if i in skiprows:
                o.write(line)
            if i == maxrow:
                return

try this,

df=pd.read_csv('input.csv')
skiprows=[1,3,6]
df,df_skiprow=df.drop(skiprows),df.iloc[skiprows]
#df_skiprow.to_csv('skiprows.csv',index=False)

Input:

    a    b
0   1   c1
1   2   c2
2   3   c3
3   4   c4
4   5   c5
5   6   c6
6   7   c7
7   8   c8
8   9   c9
9  10  c10

Output: df

    a    b
0   1   c1
2   3   c3
4   5   c5
5   6   c6
7   8   c8
8   9   c9
9  10  c10

df_skiprow

   a   b
1  2  c2
3  4  c4
6  7  c7

Explanation:

  1. read whole file.
  2. split file by df and skiprow
  3. convert into seperate csv file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM