🐼 Pandas at vs loc: различия и применение в работе с данными

When working with pandas, the at and loc labels are used for accessing and modifying data within a DataFrame or Series.

at is used to access or modify a single value in a DataFrame or Series using its label. It is fast and efficient for accessing or modifying a single value at a time. Here's an example:


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Country': ['USA', 'Canada', 'UK']}

df = pd.DataFrame(data)

# Accessing a value using at
value = df.at[0, 'Name']
print(value)  # Output: Alice

# Modifying a value using at
df.at[2, 'Age'] = 40
print(df)  # Output:
#      Name  Age Country
# 0   Alice   25     USA
# 1     Bob   30  Canada
# 2  Charlie   40      UK

loc is used to access or modify multiple values in a DataFrame or Series using labels or boolean indexing. It is more flexible and can be used to access or modify multiple values at once. Here's an example:


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'Country': ['USA', 'Canada', 'UK']}

df = pd.DataFrame(data)

# Accessing a subset of data using loc
subset = df.loc[[0, 2], ['Name', 'Country']]
print(subset)  # Output:
#       Name Country
# 0    Alice     USA
# 2  Charlie      UK

# Modifying a subset of data using loc
df.loc[1:, 'Age'] = [31, 36]
print(df)  # Output:
#       Name  Age Country
# 0    Alice   25     USA
# 1      Bob   31  Canada
# 2  Charlie   36      UK

Детальный ответ

Pandas "at" vs "loc": Understanding the Differences

When working with pandas, one often comes across the need to access and modify individual elements within a DataFrame. Two commonly used methods for this purpose are "at" and "loc". In this article, we will explore the similarities and differences between these two methods and understand when to use each of them.

The "at" Method

The "at" method in pandas allows us to access and modify a single data element within a DataFrame. It provides a fast and efficient way to handle individual elements. The syntax for using "at" is:

df.at[row_index, column_name]

Let's understand this with an example:

import pandas as pd

    # Create a sample DataFrame
    data = {'Name': ['John', 'Emma', 'Peter'],
            'Age': [25, 30, 35],
            'City': ['New York', 'London', 'Sydney']}

    df = pd.DataFrame(data)

    # Access and modify a single element using "at"
    df.at[1, 'Age'] = 31

    print(df)

This will output:

    Name    Age      City
    0   John    25  New York
    1   Emma    31    London
    2  Peter    35    Sydney

As you can see, we used the "at" method to modify the age of the second person from 30 to 31.

The "loc" Method

The "loc" method in pandas is used to access and modify a subset of the DataFrame based on some conditions or criteria. It provides a powerful way to select specific rows and columns. The syntax for using "loc" is:

df.loc[row_indexer, column_indexer]

Let's look at an example:

# Using "loc" to access and modify a subset of the DataFrame
    df.loc[df['Age'] > 30, 'Age'] = 40

    print(df)

This will output:

    Name  Age      City
    0   John   25  New York
    1   Emma   40    London
    2  Peter   40    Sydney

Here, we used the "loc" method to select all rows where the age is greater than 30 and update their ages to 40.

Differences between "at" and "loc"

Now that we have seen the basic usage of both "at" and "loc" methods, let's understand the main differences between them:

  • Functionality: "at" method is used to access or modify individual data elements, while "loc" method is used to select and modify subsets of the DataFrame.
  • Performance: "at" method is faster than "loc" as it directly accesses a single element, while "loc" involves selection and modification of multiple elements.
  • Syntax: "at" uses two parameters - row index and column name, whereas "loc" uses indexers for both rows and columns, supports slicing, and provides more flexibility.

Conclusion

Both "at" and "loc" methods serve different purposes when it comes to accessing and modifying data in a pandas DataFrame. If you need to access or modify a single element, "at" is a better choice due to its speed and efficiency. On the other hand, if you need to select and modify subsets of data based on certain conditions, "loc" is the method to use.

Remember to choose the method that suits your specific requirements and consider the trade-offs between performance and functionality. With a good understanding of "at" and "loc", you can efficiently work with pandas DataFrames and manipulate data as per your needs.

Видео по теме

Pandas loc and iloc | Pandas loc tutorial | Pandas iloc slicing | loc vs iloc

Обращение к элементам Series. Методы loc и iloc

Difference between loc and iloc in Pandas explained.

Похожие статьи:

Как фильтровать строки в pandas по значению?

🐼 Pandas at vs loc: различия и применение в работе с данными

Как добавить новый бинарный признак в pandas: пошаговое руководство для начинающих