📜  Python|熊猫系列.iloc(1)

📅  最后修改于: 2023-12-03 14:46:31.118000             🧑  作者: Mango

Python Pandas Series with .iloc

Pandas is one of the most popular and powerful data analysis libraries in Python. One of the key components of Pandas is Series, which is a one-dimensional array of labeled data. Series is very similar to a Python list, but with extra functionality.

The .iloc property in Pandas Series is used to access data using integer-based indexing. It allows you to select a single value or a subset of values from a Series based on their position rather than their label.

Accessing a Single Value

To access a single value from a Pandas Series using .iloc, you can pass the index position of the value you want to the .iloc property. For example:

import pandas as pd

s = pd.Series([10, 20, 30, 40, 50])
value = s.iloc[2]

print(value)  # Output: 30

In this example, we create a Pandas Series with five values and then use .iloc[2] to access the value at index position 2.

Accessing a Subset of Values

To access a subset of values from a Pandas Series using .iloc, you can pass a list of index positions to the .iloc property. For example:

import pandas as pd

s = pd.Series([10, 20, 30, 40, 50])
subset = s.iloc[[1, 3, 4]]

print(subset)  # Output: 1    20
               #         3    40
               #         4    50
               #         dtype: int64

In this example, we create a Pandas Series with five values and then use .iloc[[1, 3, 4]] to access the values at index positions 1, 3, and 4.

Conclusion

The .iloc property in Pandas Series is a powerful tool for accessing data using integer-based indexing. Whether you need to retrieve a single value or a subset of values, .iloc makes it easy to retrieve the data you need. Try it out in your next data analysis project!