Loryws
Loryws

Reputation: 15

Select some columns in a csv file in pandas

I have to read some columns/lines from a csv file in Python. I thought to use Pandas, but i don't reach to do this: Read the columns between number 8 and number 11 (included), and the lines between number 25 and 30. I have already seen here

How to use pandas to select certain columns in csv file

But it hasn't helped me so far. Thank you for your help.

import pandas as pd
df = pd.read_csv('panels.csv' , usecols = ['col1','col2'])
df

Upvotes: 1

Views: 171

Answers (3)

jezrael
jezrael

Reputation: 862641

Use numpy.r_ for concanecate indices:

df = pd.read_csv('panels.csv' , usecols = ['col1','col2'])

df = df.iloc[np.r_[8:12, 25:31]]

Or if want use only iloc function for seelct by positions is necessary Index.get_indexer for positions of columns names:

df = pd.read_csv('panels.csv')

df = df.iloc[np.r_[8:12, 25:31], df.columns.get_indexer(['col1','col2'])]

Sample:

np.random.seed(2019)
df = pd.DataFrame(np.random.randint(10, size=(35, 3)), columns=['col1','col2', 'col3'])
df.to_csv('panels.csv', index=False)

df1 = pd.read_csv('panels.csv' , usecols = ['col1','col2'])

df1 = df1.iloc[np.r_[8:12, 25:31]]
print (df1)
    col1  col2
8      6     6
9      1     3
10     0     2
11     1     8
25     7     0
26     9     0
27     2     9
28     4     7
29     0     5
30     4     1

df2 = pd.read_csv('panels.csv')

df2 = df2.iloc[np.r_[8:12, 25:31], df2.columns.get_indexer(['col1','col2'])]
print (df2)
    col1  col2
8      6     6
9      1     3
10     0     2
11     1     8
25     7     0
26     9     0
27     2     9
28     4     7
29     0     5
30     4     1

df3 = pd.read_csv('panels.csv')

df3 = df3.loc[df3.index[np.r_[8:12, 25:31]], ['col1','col2']]
print (df3)
    col1  col2
8      6     6
9      1     3
10     0     2
11     1     8
25     7     0
26     9     0
27     2     9
28     4     7
29     0     5
30     4     1

Upvotes: 1

freak7
freak7

Reputation: 99

If you wan't to select columns between number 8 and number 11, and the lines between number 25 and 30 then try using the below code.

data_n = data.loc[8:12, 'Line25':'Line31']

Upvotes: 0

Shishir Naresh
Shishir Naresh

Reputation: 763

Try the following, it is in general approach. You can change accordingly. I Hope this would help.

data[['Rank','Title']].iloc[list(range(8,12))+list(range(25,31))]

Here instead of Rank and Title, you can pass whatever column names you want to select.

Upvotes: 0

Related Questions