Pandas pd.Series() Function
pd.Series()It is a core function in the Pandas library, used to create one-dimensional arrays (similar to lists or NumPy arrays), but it is more powerful than ordinary arrays.SeriesIt can store any data type (such as integers, strings, floating-point numbers, etc.), and each element has an associated index, which makes data operations more flexible.
Basic syntax of pd.Series()
pd.Series(data, index=None, dtype=None, name=None, copy=False)
Parameter description:
- data: data, can be a list, dictionary, NumPy array, etc.
- index: index, used to label data. If not specified, it defaults to starting from 0.
- dtype: data type, such as
int、float、stretc. - name: the name of the Series, usually used to label data.
- copy: whether to copy data, defaults to
False。
How to use pd.Series()?
Example 1: Create a Series from a list
Example
import pandas as pd
# Create a simple Series
data = [10, 20, 30, 40]
s = pd.Series(data)
print(s)
# Create a simple Series
data = [10, 20, 30, 40]
s = pd.Series(data)
print(s)
Output:
0 10 1 20 2 30 3 40 dtype: int64
Explanation:
- data
[10, 20, 30, 40]is converted to a Series. - The default index starts from 0.
Example 2: Specify index
Example
# Create a Series and specify the index
data = [10, 20, 30, 40]
index = ['a', 'b', 'c', 'd']
s = pd.Series(data, index=index)
print(s)
data = [10, 20, 30, 40]
index = ['a', 'b', 'c', 'd']
s = pd.Series(data, index=index)
print(s)
Output:
a 10 b 20 c 30 d 40 dtype: int64
Explanation:
- The index is specified as
['a', 'b', 'c', 'd'], instead of the default 0, 1, 2, 3.
Example 3: Create a Series from a dictionary
Example
# Create a Series from a dictionary
data = {'a': 10, 'b': 20, 'c': 30, 'd': 40}
s = pd.Series(data)
print(s)
data = {'a': 10, 'b': 20, 'c': 30, 'd': 40}
s = pd.Series(data)
print(s)
Output:
a 10 b 20 c 30 d 40 dtype: int64
Explanation:
- The dictionary's keys automatically become the Series indices, and the values become the data.
Example 4: Specify data type
Example
# Create a Series and specify the data type as float
data = [10, 20, 30, 40]
s = pd.Series(data, dtype=float)
print(s)
data = [10, 20, 30, 40]
s = pd.Series(data, dtype=float)
print(s)
Output:
0 10.0 1 20.0 2 30.0 3 40.0 dtype: float64
Explanation:
- The data type is specified as
float, so all values are displayed as floating-point numbers.
Common operations of Series
1. Access data
You can access data in a Series by index.
Example
s = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])
print(s['b']) # Output: 20
print(s['b']) # Output: 20
2. Modify data
You can modify data in a Series by index.
Example
s['b'] = 25
print(s)
print(s)
Output:
a 10 b 25 c 30 d 40 dtype: int64
3. Slicing operations
You can perform slicing operations on a Series.
Example
print(s['b':'d'])
Output:
b 25 c 30 d 40 dtype: int64
4. Mathematical operations
You can perform mathematical operations on a Series.
Example
print(s * 2)
Output:
a 20 b 50 c 60 d 80 dtype: int64Other extensions
Pandas Series API Manual