JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Querying a column from multiple columns based on value in Pandas

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

In this tutorial, you will learn how to perform lookup operations in Pandas.


Steps to find from one of multiple columns based on value in Pandas

Following are the steps to lookup from one of multiple columns based on the value in Pandas DataFrame.

Import Pandas

We will now import the basic libraries we need to get started.

import pandas as pd

Creating a Pandas DataFrame

We will create a sample DataFrame that we will use to perform the lookup process.

data = {
    "Year": ["2000", "2001", "2002", "2003"],
    "data": ["a", "b", "c", "d"],
    "a": [1, 2, 3, 4],
    "b": [5, 6, 7, 8],
    "c": [9, 10, 11, 12],
    "d": [13, 14, 15, 16],
}
df = pd.DataFrame(data)

In the code above, we create a datadictionary of lists called . We then pass this dictionary to pd.DataFrame()the function to create a Pandas DataFrame.

Now let's see how our DataFrame looks like.

print(df)

Output:

   Year data  a  b   c   d
0  2000    a  1  5   9  13
1  2001    b  2  6  10  14
2  2002    c  3  7  11  15
3  2003    d  4  8  12  16

Use lookup()the function to find a value from one of multiple columns

We will now dataperform a lookup from one of the multiple columns based on the column value. We will use the function in Pandas lookup()to perform the required operation.

df["value"] = df.lookup(df.index, df["data"])

In the above code, we have added a valuenew column called with lookup()the lookup value added by the function. In the lookup function, we pass the column name whose index value we want to find in the following columns.

valueWe now print the updated DataFrame with the newly added column with the lookup value .

print(df)

Output:

   Year data  a  b   c   d  value
0  2000    a  1  5   9  13      1
1  2001    b  2  6  10  14      6
2  2002    c  3  7  11  15     11
3  2003    d  4  8  12  16     16

We have successfully added a new column with the lookup value in the above output. Hence, we can successfully find the lookup value in Pandas DataFrame by the above method.

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

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial