
```python
import pandas as pd
data = [1, 3, 5, 7, 9]
series = pd.Series(data)
print(series)
```
```python
#Output
0 1
1 3
2 5
3 7
4 9
dtype: int64
```import pandas as pd
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
print(df)#Output
A B C
0 1 4 7
1 2 5 8
2 3 6 9
| Dimensionality | Structure | Data Type | Usage | |
|---|---|---|---|---|
| Series | One-dimensional | A single sequence of values, similar to a list of an array | Typically homogeneous (all elements are of the same type) | Useful for storing and manipulating columns and for operations involving multiple variables |
| DataFrame | Two-dimensional | A table with multiple columns, each of which can be considered a ‘Series’ | Heterogeneous (different columns can have different types) | Ideal for datasets with multiple columns and for operations involving multiple variables |
Definition: ‘iloc’ stands for ‘integer location’ and is used for indexing by position. It allows you to select data by its integer position.
Characteristics
Usage : Access rows and columns using Integer indices.
Example code
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
# Select the first row
print(df.iloc[0])
# Select the first row and first column
print(df.iloc[0, 0])
# Select the first two rows
print(df.iloc[:2])
# Select the first two rows and first two columns
print(df.iloc[:2, :2])
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data, index=['one', 'two', 'three'])
# Select the row with label 'one'
print(df.loc['one'])
# Select the row with label 'one' and column 'A'
print(df.loc['one', 'A'])
# Select rows with labels 'one' and 'two'
print(df.loc[['one', 'two']])
# Select rows 'one' and 'two' and columns 'A' and 'B'
print(df.loc[['one', 'two'], ['A', 'B']])| Feature | ‘iloc’ | ‘loc’ |
|---|---|---|
| Indexing Method | Integer-Based | Label-based |
| Slicing Behavior | Start inclusive, end exclusive | Both start and end inclusive |
| Error Handling | ‘IndexError’ for out-of-bounds | ‘KeyError’ for missing labels |
| Access Method | Positional | Label |
| Usage | When position is known | When label is known |
| Supports Boolean Arrays | No | Yes |
| Supports Integer Arrays | Yes | No |
| Flexibility | Less flexible | More flexible |