Pandas all articles except the latest

I have a csv file, is it possible to have usecols all columns except the last when using read_csv without specifying each column.

For example, if I have a file with 13 columns, I can do usecols=[0,1,...,10,11] . Doing usecols=[:-1] give me a syntax error?

Is there any other alternative? I am using pandas 0.17

+5
source share
2 answers

You can simply read one row using nrows=1 to get cols, and then re-read the full csv, skipping the last col by cutting off the column array from the first read:

 cols = pd.read_csv(file, nrows=1).columns df = pd.read_csv(file, usecols=cols[:-1]) 
+4
source

Starting with version 0.20 the usecols method in pandas accepts a called filter, i.e. lambda expression. Therefore, if you know the name of the column that you want to skip, you can do the following:

 columns_to_skip = ['foo','bar'] df = pd.read_csv(file, usecols=lambda x: x not in columns_to_skip ) 

Here is the documentation link .

+1
source

Source: https://habr.com/ru/post/1234831/


All Articles