JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Selecting Multiple Columns in a Pandas Dataframe

Author:JIYIK Last Updated:2025/05/02 Views:

We may face some problems when extracting multiple columns of data from Pandas DataFrame, mainly because they treat Dataframe as a two-dimensional array. To select multiple columns of data from a DataFrame, we can use basic indexing methods, pass a list of column names to __getitem__the syntax ( ), or use the and methods []provided by the Pandas library . In this tutorial, we will select multiple columns from the following DataFrame.iloc()loc()

Example DataFrame:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(4, 4), columns=["a", "b", "c", "d"])

print(df)

Output:

          a         b         c         d
0  0.255086  0.282203  0.342223  0.263599
1  0.744271  0.591687  0.861554  0.871859
2  0.420066  0.713664  0.770193  0.207427
3  0.014447  0.352515  0.535801  0.119759

Use __getitem__the syntax ( []) to select multiple columns

[]We can select multiple columns from a DataFrame by storing the column names to be extracted in a list and then passing it to . The following code will explain how we can select the columns aand from the DataFrame shown previously c.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(4, 4), columns=["a", "b", "c", "d"])

print(df[["a", "c"]])

Output:

          a         c
0  0.255086  0.342223
1  0.744271  0.861554
2  0.420066  0.770193
3  0.014447  0.535801

Selecting Multiple Columns in Pandas Using the iloc()and Methodsloc()

We can also use the iloc()and loc()methods to select multiple columns.

When we want to extract them using column index, we can use iloc()as shown in the following example.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(4, 4), columns=["a", "b", "c", "d"])
print(df.iloc[:, [0, 2]])

Output:

          a         c
0  0.255086  0.342223
1  0.744271  0.861554
2  0.420066  0.770193
3  0.014447  0.535801

Similarly, when we want to select columns using column names, we can use loc()as shown in the following image.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(4, 4), columns=["a", "b", "c", "d"])

print(df.loc[:, ["a", "c"]])

Output:

          a         c
0  0.255086  0.342223
1  0.744271  0.861554
2  0.420066  0.770193
3  0.014447  0.535801

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:147 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

How to Add a Row to a Pandas DataFrame

Publish Date:2025/05/02 Views:127 Category:Python

Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to

How to change the order of Panas DataFrame columns

Publish Date:2025/05/02 Views:184 Category:Python

We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns

How to pretty print an entire Pandas Series/DataFrame

Publish Date:2025/05/02 Views:167 Category:Python

We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option

How to Convert a Pandas Dataframe to a NumPy Array

Publish Date:2025/05/02 Views:151 Category:Python

We will introduce to_numpy() the method to pandas.Dataframe convert a to NumPy an array, which is introduced in pandas v0.24.0, replacing the old .values method. We can define it on Index , Series , and DataFrame objects to_numpy . The old

How to add a header row to a Pandas DataFrame

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for adding a header row to a pandas dataframe, as well as the option to pass in the names directly in the dataframe or by assigning the column names in a list directly to dataframe.columns the method. We will also in

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial