Julia
Julia

Reputation: 176

Python Removing Columns

I am trying to remove the last two columns from my data frame by using Python.

The issue is there are cells with values in the last two columns that we don't need, and those columns don't have headers.

Here's the code I wrote, but I'm really new to Python, and don't know how to take my original data and remove the last two columns.

import csv
with open("Filename","rb") as source:
    rdr= csv.reader( source )
    with open("Filename","wb") as result:
        wrt= csv.writer ( result )
        for r in rdr:
            wrt.writerow( (r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11]) )

Thanks!

Upvotes: 1

Views: 149

Answers (1)

Steven Hewitt
Steven Hewitt

Reputation: 302

The proper Pythonic way to perform something like this is through slicing:

r[start:stop(:step)]

start and stop are indexes, where positive indexes are counted from the front and negative is counted from the end. Blank starts and stops are treated as the beginning and the end of r respectively. step is an optional parameter that I'll explain later. Any slice returns an array, which you can perform additional operations on or just return immediately.

In order to remove the last two values, you can use the slice

r[:-2]

Additional fun with step

Now that step parameter. It allows you to pick every stepth value from the selected slice. With an array of, say, r = [0,1,2,3,4,5,6,7,8,9,10] you can pick every other number starting with the first (all of the even numbers) with the slice r[::2]. In order to get results in reverse order, you can make the step negative:

> r = [0,1,2,3,4,5,6,7,8,9,10]
[0,1,2,3,4,5,6,7,8,9,10]
> r[::-1]
[10,9,8,7,6,5,4,3,2,1,0]

Upvotes: 3

Related Questions