Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Rows by integers and Columns by labels Pandas

My data is like this:

[First row is headers]

Name,Email,Age
Sachith,[email protected],23
Sim,[email protected],234
Yoshi,[email protected],2345
sarla,[email protected],234

I would like to access elements such that rows are specified as integers and columns by labels. i.e for Sim I would like to access it as [1,'Name'] and so-on

My question is should I use loc or ix?

Looking at the documentation, I am confused as to what is a pandas index? Is it used to access rows or columns or both? When I try to print the indices of this data, I get a (4,) dtype=int64 array of [0,1,2,3]. So , are columns not part of the index?

like image 300
sir_osthara Avatar asked Jul 26 '17 14:07

sir_osthara


People also ask

How can I get specific rows and columns in Pandas?

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc. Indexing in Pandas means selecting rows and columns of data from a Dataframe.

How do I select specific rows and columns from a DataFrame?

To select a single value from the DataFrame, you can do the following. You can use slicing to select a particular column. To select rows and columns simultaneously, you need to understand the use of comma in the square brackets.

How do I select a specific row in a DataFrame by index in Python?

To select the rows, the syntax is df. loc[start:stop:step] ; where start is the name of the first-row label to take, stop is the name of the last row label to take, and step as the number of indices to advance after each extraction; for example, you can use it to select alternate rows.

How to select rows&columns by name or index in pandas?

Select Rows & Columns by Name or Index in Pandas DataFrame using [ ], loc & iloc. Indexing in Pandas means selecting rows and columns of data from a Dataframe. It can be selecting all the rows and the particular number of columns, a particular number of rows, and all the columns or a particular number of rows and columns each.

How do I access rows in a pandas Dataframe?

The steps explained ahead are related to the sample project introduced here. You can use the loc and iloc functions to access rows in a Pandas DataFrame. Let’s see how. In our DataFrame examples, we’ve been using a Grades.CSV file that contains information about students and their grades for each lecture they’ve taken:

How to select rows and columns in pandas chain operation?

We can use Pandas chain operation with multiple filter () functions to select rows and columns or columns and rows of interest. (df. filter(regex='^f', axis="index").

What is the row index of toy Dataframe in pandas?

Our toy dataframe’ row index is integer. For the sake of easiness, let us change the row index to be string instead of numbers in order. Let us assign new row index names using “index” method in Pandas.


1 Answers

Use loc or iloc, because ix is deprecated.

print (df)
      Name             Email   Age
0  Sachith      [email protected]    23
1      Sim      [email protected]   234
2    Yoshi  [email protected]  2345
3    sarla   [email protected]   234

#select by label 1 and label Name    
a = df.loc[1, 'Name']
print (a)
Sim

But if need select index by position (need iloc) and columns by labels (need loc) together:

df = df.set_index('Email')
print (df)
                     Name   Age
Email                          
[email protected]      Sachith    23
[email protected]          Sim   234
[email protected]    Yoshi  2345
[email protected]     sarla   234

get label of second index by df.index[1]:

a = df.loc[df.index[1], 'Name']
print (a)
Sim

Or get position of label by get_loc:

a = df.iloc[1, df.columns.get_loc('Name')]
print (a)
Sim
like image 84
jezrael Avatar answered Nov 10 '22 16:11

jezrael