Meta Description
Learn basic functionality of Pandas, including Series and DataFrame attributes, methods, and how to manipulate data effectively in Python.
Working with Attributes in Pandas
Attributes in Pandas allow you to access metadata about your Series and DataFrame objects. By using these attributes, you can explore and easily understand the data.
Series and DataFrame Attributes
| Sr.No. | Attribute & Description |
|---|---|
| 1 | dtype – Returns the data type of the elements in the Series or DataFrame. |
| 2 | index – Provides the index (row labels) of the Series or DataFrame. |
| 3 | values – Returns the data in the Series or DataFrame as a NumPy array. |
| 4 | shape – Returns a tuple representing the dimensionality of the DataFrame (rows, columns). |
| 5 | ndim – Returns the number of dimensions of the object. Series is always 1D, and DataFrame is 2D. |
| 6 | size – Gives the total number of elements in the object. |
| 7 | empty – Checks if the object is empty, and returns True if it is. |
| 8 | columns – Provides the column labels of the DataFrame object. |
Example 1: Series Attributes
import pandas as pd
import numpy as np
# Create a Series with random numbers
s = pd.Series(np.random.randn(4))
# Exploring attributes
print("Data type of Series:", s.dtype)
print("Index of Series:", s.index)
print("Values of Series:", s.values)
print("Shape of Series:", s.shape)
print("Number of dimensions of Series:", s.ndim)
print("Size of Series:", s.size)
print("Is Series empty?:", s.empty)
Output:
Data type of Series: float64
Index of Series: RangeIndex(start=0, stop=4, step=1)
Values of Series: [-1.02016329 1.40840089 1.36293022 1.33091391]
Shape of Series: (4,)
Number of dimensions of Series: 1
Size of Series: 4
Is Series empty?: False
Example 2: DataFrame Attributes
import pandas as pd
import numpy as np
# Create a DataFrame with random numbers
df = pd.DataFrame(np.random.randn(3, 4), columns=list('ABCD'))
print("DataFrame:")
print(df)
print("Results:")
print("Data types:", df.dtypes)
print("Index:", df.index)
print("Columns:", df.columns)
print("Values:")
print(df.values)
print("Shape:", df.shape)
print("Number of dimensions:", df.ndim)
print("Size:", df.size)
print("Is empty:", df.empty)
Output:
DataFrame:
A B C D
0 2.161209 -1.671807 -1.020421 -0.287065
1 0.308136 -0.592368 -0.183193 1.354921
2 -0.963498 -1.768054 -0.395023 -2.454112
Results:
Data types:
A float64
B float64
C float64
D float64
dtype: object
Index: RangeIndex(start=0, stop=3, step=1)
Columns: Index(['A', 'B', 'C', 'D'], dtype='object')
Values:
[[ 2.16120893 -1.67180742 -1.02042138 -0.28706468]
[ 0.30813618 -0.59236786 -0.18319262 1.35492058]
[-0.96349817 -1.76805364 -0.3950226 -2.45411245]]
Shape: (3, 4)
Number of dimensions: 2
Size: 12
Is empty: False
Exploring Basic Methods in Pandas
Pandas offers several basic methods in both the Series and DataFrame data structures, making it easy to quickly look at and understand your data. These methods help you get a summary and explore the details without much effort.
Series and DataFrame Methods
| Sr.No. | Method & Description |
|---|---|
| 1 | head(n) – Returns the first n rows of the object. The default value of n is 5. |
| 2 | tail(n) – Returns the last n rows of the object. The default value of n is 5. |
| 3 | info() – Provides a concise summary of a DataFrame, including the index dtype, column dtypes, non-null values, and memory usage. |
| 4 | describe() – Generates descriptive statistics of the DataFrame or Series, such as count, mean, std, min, and max. |
Example 3: Series Methods
import pandas as pd
import numpy as np
# Create a Series with random numbers
s = pd.Series(np.random.randn(10))
print("Series:")
print(s)
# Using basic methods
print("First 5 elements of the Series:\n", s.head())
print("\nLast 3 elements of the Series:\n", s.tail(3))
print("\nDescriptive statistics of the Series:\n", s.describe())
Output:
Series:
0 -0.295898
1 -0.786081
2 -1.189834
3 -0.410830
4 -0.997866
5 0.084868
6 0.736541
7 0.133949
8 1.023674
9 0.669520
dtype: float64
First 5 elements of the Series:
0 -0.295898
1 -0.786081
2 -1.189834
3 -0.410830
4 -0.997866
dtype: float64
Last 3 elements of the Series:
7 0.133949
8 1.023674
9 0.669520
dtype: float64
Descriptive statistics of the Series:
count 10.000000
mean -0.103196
std 0.763254
min -1.189834
25% -0.692268
50% -0.105515
75% 0.535627
max 1.023674
dtype: float64
Example 4: DataFrame Methods
import pandas as pd
import numpy as np
#Create a Dictionary of series
data = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(data)
print("Our data frame is:\n")
print(df)
# Using basic methods
print("\nFirst 5 rows of the DataFrame:\n", df.head())
print("\nLast 3 rows of the DataFrame:\n", df.tail(3))
print("\nInfo of the DataFrame:")
df.info()
print("\nDescriptive statistics of the DataFrame:\n", df.describe())
Output:
Our data frame is:
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
3 Vin 23 2.56
4 Steve 30 3.20
5 Smith 29 4.60
6 Jack 23 3.80
First 5 rows of the DataFrame:
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
3 Vin 23 2.56
4 Steve 30 3.20
Last 3 rows of the DataFrame:
Name Age Rating
4 Steve 30 3.2
5 Smith 29 4.6
6 Jack 23 3.8
Info of the DataFrame:
RangeIndex: 7 entries, 0 to 6
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 7 non-null object
1 Age 7 non-null int64
2 Rating 7 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 231.0+ bytes
Descriptive statistics of the DataFrame:
Age Rating
count 7.000000 7.000000
mean 26.000000 3.658571
std 2.516611 0.696669
min 23.000000 2.560000
25% 23.000000 3.080000
50% 25.000000 3.800000
75% 27.000000 4.050000
max 30.000000 4.600000
