Pandas DataFrame loc Multiple Columns
Pandas is a powerful data manipulation library in Python. It provides data structures and functions needed to manipulate structured data. One of the most commonly used data structures in pandas is DataFrame, which is a two-dimensional labeled data structure with columns of potentially different types.
In this article, we will focus on the loc
function in pandas DataFrame. The loc
function is a label-based data selection method which means that we have to pass the name of the row or column which we want to select. This method includes the last element of the range. Unlike, the iloc
function which is an index-based selection method and it does not include the last element of the range.
More specifically, we will discuss how to use loc
to select multiple columns in a DataFrame. We will provide 10-20 complete, context-independent, and directly runnable pandas codes.
Creating a DataFrame
Before we dive into the usage of loc
, let’s first create a DataFrame. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df)
Output:
Selecting a Single Column with loc
The loc
function can be used to select a single column. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[:, 'Name'])
Output:
Selecting Multiple Columns with loc
The loc
function can also be used to select multiple columns. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[:, ['Name', 'Age']])
Output:
Selecting Multiple Columns with a Slice
The loc
function can also select multiple columns with a slice. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[:, 'Name':'Age'])
Output:
Selecting Multiple Columns with a Boolean Array
The loc
function can also select multiple columns with a Boolean array. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[:, [True, False, True]])
Output:
Selecting Multiple Columns with a Callable
The loc
function can also select multiple columns with a callable. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[:, lambda df: ['Name', 'Age']])
Output:
Selecting Rows and Columns with loc
The loc
function can also select rows and columns at the same time. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[0:2, ['Name', 'Age']])
Output:
Selecting Rows and Columns with a Callable
The loc
function can also select rows and columns with a callable. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[lambda df: df.Age > 30, ['Name', 'Age']])
Output:
Selecting Rows and Columns with a Boolean Array
The loc
function can also select rows and columns with a Boolean array. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[df.Age > 30, [True, False, True]])
Output:
Selecting Rows and Columns with a Slice
The loc
function can also select rows and columns with a slice. Here is an example:
import pandas as pd
data = {
'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']
}
df = pd.DataFrame(data)
print(df.loc[0:2, 'Name':'Age'])
Output:
Pandas DataFrame loc Multiple Columns Conclusion
In this article, we have discussed how to use the loc
function in pandas DataFrame to select multiple columns. We have provided 10 complete, context-independent, and directly runnable pandas codes. We hope this article can help you understand and use the loc
function better. For more information, please visit pandasdataframe.com.