| by Arround The Web | No comments

Pandas at Function

“Python is one of the best languages for analyzing the data and results, largely due to the strong environment of python packages that are focused on data handling. One of those tools that greatly simplify the process of importing and evaluating data is Pandas. The Pandas at[] function is used to return data inside a dataframe at the passed point. [position, Column Name] is the format of the passed location. This method works similarly to Pandas loc[], except at[] returns a single value and so executes more quickly. With the help of the Pandas Series.at attribute, we may get at a single value for a set of row/column labels. This attribute and loc both offer label-based lookups; hence they are similar.”

How to Use the Pandas .at function?

In Pandas, the at method is used to extract specific data or values from a dataframe and to extract values from the series object. To use the .at function for series and dataframe, we have to follow its syntax. Let’s look at their syntax first before using it to extract data.

Syntax of .at function on Dataframes

The syntax of the dataframe.at function is as follows:

Syntax: Dataframe.at[position, label]

Where,

position: Position of the element in the dataframe column.

label: Name of the column to be used.

Return value: The value located there is returned by the .at method.

Errors: If the arguments given as the column labels and row index are outside of bounds or are missing from the dataframe, the key error is raised.

Syntax of astype() function on Series

The syntax of series.at function is as follows:

Syntax: Series.at[]

Parameter: None

There is no parameter required for the series.at method, we can get the value just by using the value which we want to extract inside the parenthesis.

Returns: single value is passed.

KeyError will rise if the label in the series is absent.

Now, we have seen the syntax of the .at function with series and dataframe. We will implement the .at function with series and data frame in the following examples, so you will learn how to use the .at function by yourself.

Example # 1: Retrieve Data by Index and Column Names in Dataframe Using DataFrame.at[] function

Now let’s build a DataFrame with several rows and columns so we can demonstrate our example and examine the output. Before creating the dataframe, we will import the module of pandas. Pandas are one of the most widely used tools in data science and machine learning for cleaning and analyzing data. The Pandas is the ideal tool for handling this chaotic real-world data.


The pandas modules have been imported into the script above. We created multiple lists holding the records of sportsmen. Then we created a dataframe using the pd.Dataframe() function. Inside the pd.DataFrame function, we have passed our lists (players), columns containing the name for each column of the dataframe, and index for naming the index of our df dataframe. To illustrate our df data frame, we will use the print() function, and inside it, we will pass our dataframe df as an input.


You can see there are 4 columns name, age, sports, and country showing the data of six sportsmen of different sports who belong to different countries.

Now, we will retrieve the value of the element of column name “sports” at row name “2”.


As you can see, it retrieved Sports “badminton” from the row at index two. Let’s consider the example where we attempt to retrieve the value of a cell by providing a row name that isn’t present.


The console is displaying a key error because, in our dataframe, there is no index with the name “7”.

Example # 2: Retrieve Data From the Dataframe by Using Variable Name

First of all, we will import the pandas library into the current environment. We will use the same dataframe df in this example, which was created in example # 1.


After importing pandas, we have created 2 variables loc for storing the index position and col for storing the column name. We assigned 4 to the loc variable and “Name” to the col variable, which means it has to retrieve data from the 4th index of the Name column. To examine the result from the DataFrame.at function, we can print the retrieved value by passing its variable name(i.e., get) in the print() function.


The output may be compared, as seen in the output, and it can be noted that the value in the Name column at the fourth position is similar to the output.

This .at[] only returns a single value, unlike.loc[]. The Dataframe.at[ 3:6, col ] will therefore return an error. This approach is quicker than. loc[] because it only applies to single values.

Example # 3: Specify or Overwrite a Value to a Specific Row/Column Using DataFrame.at[]

We will use the df datafame, which was created in example # 1. First, we will check the data in our df dataframe by using the print() function.

Let’s suppose we need to change the sports in row 5. As you can see, currently the sport in row 5 is tennis. We will modify the cell value in this example. Using Data.Frame.at[]. To overwrite or change the data, we have to access that specific cell first.


We have successfully accessed the required cell. Now, we will set a new value in this cell by using DataFrame.at[] function.


We have changed the value from “tennis” to “basketball”. You can see the output by printing our df dataframe.


You can observe that the value in row 5 and column Sports is changed to basketball. Using .at[], you can only set the value of a column or row which exists in our dataframe; otherwise, it will display an error on the console.

Example # 4: Retrieve a Single Value at any Particular Place in a Series Using series.at[] attribute

We have seen from the examples below how you can extract single-cell data from .at[]. Now, in this example, we will get the value from the series using the series.at[] attribute. First, we will create an object series after importing the pandas module.


As you can see, we created a series containing the names of persons. To visualize our series, the print() function can be used.


We have a series of 6 names, indexing from 0 to 5. To retrieve the data from a specific location in a series, we can simply pass the index inside the parentheses of the series.at[] attribute. Let’s retrieve the value at index location 4.


Leo’ is the value that is listed at position 4 in the supplied Series object, as can be seen in the output from the Series.at attribute. We can also set a value at a specified index location in series using series.at[] as we have done in example # 3.

Conclusion

In this article, we have discussed how to use the .at[] property to select or get an individual cell value or data from a pandas DataFrame. We implemented different examples in this article using DataFrame.at[] and series.at[] attribute so you may learn how to retrieve data by index and column names in the dataframe, and retrieve data from the dataframe by using a variable name, specify or overwrite a value to a specific row/column.

Share Button

Source: linuxhint.com

Leave a Reply