pandas - Series
1、 创建Series
import pandas as pd
def createSeries():
data = [32.5, 32.6, 33, 32.1, 36]
index = ["2023-07-01", "2023-07-02", "2023-07-03", "2023-07-04", "2023-07-05"]
series_1 = pd.Series(data)
print(series_1)
series_2 = pd.Series(data, dtype=float)
print(series_2)
series_3 = pd.Series(data=data)
print(series_3)
series_4 = pd.Series(data=data, dtype=float)
print(series_4)
series_5 = pd.Series(data, index)
print(series_5)
series_6 = pd.Series(data=data, index=index)
print(series_6)
series_7 = pd.Series(data=data, index=index, dtype=float)
print(series_7)
series_1、series_2、series_3、series_4 返回结果:
2023-07-01 32.5
2023-07-02 32.6
2023-07-03 33.0
2023-07-04 32.1
2023-07-05 36.0
dtype: float64
series_5、series_6、series_7 返回结果:
2023-07-01 32.5
2023-07-02 32.6
2023-07-03 33.0
2023-07-04 32.1
2023-07-05 36.0
dtype: float64
2、分割Series
import pandas as pd
def splitSeries():
data = [32.5, 32.6, 33, 32.1, 36]
index = ["2023-07-01", "2023-07-02", "2023-07-03", "2023-07-04", "2023-07-05"]
series = pd.Series(data=data, index=index)
split_02 = series['2023-07-02']
print(split_02)
split_0204 = series['2023-07-02':'2023-07-04']
print(split_0204)
split_0102 = series[1: 3]
print(split_0304)
split_02 返回结果:
32.6
split_0204 返回结果:
2023-07-02 32.6
2023-07-03 33.0
2023-07-04 32.1
dtype: float64
split_0102 返回结果:
2023-07-02 32.6
2023-07-03 33.0
dtype: float64
3、 获取Series的Index和value
import pandas as pd
def getSeriesIndexOrValue():
data = [32.5, 32.6, 33, 32.1, 36]
index = ["2023-07-01", "2023-07-02", "2023-07-03", "2023-07-04", "2023-07-05"]
series = pd.Series(data=data, index=index)
index = series.index
print(index)
values = series.values
print(values)
index 返回结果:
Index(['2023-07-01', '2023-07-02', '2023-07-03', '2023-07-04', '2023-07-05'], dtype='object')
values 返回结果:
[32.5 32.6 33. 32.1 36. ]
4、利用索引合并数据
import pandas as pd
def seriesAutoDataProcess():
"""
利用索引合并数据
:return:
"""
index_1 = ['i1', 'i2', 'i3', 'i4']
data_1 = [100, 200, 300, 400]
index_2 = ['i2', 'i3', 'i4', 'i5']
data_2 = [20, 30, 40, 50]
series_1 = pd.Series(data=data_1, index=index_1)
series_2 = pd.Series(data=data_2, index=index_2)
print(series_1 + series_2)
返回结果:
i1 NaN
i2 220.0
i3 330.0
i4 440.0
i5 NaN
dtype: float64
5、利用索引填充数据
import pandas as pd
def seriesDataProcess():
data = [11, 22, 33, 44, 66]
index = ['i1', 'i2', 'i3', 'i4', 'i6']
reindex = ['i0', 'i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7']
series = pd.Series(data=data, index=index)
print(series)
# 对Series对象重新设置索引,非交集设置为NaN
series_reindex = series.reindex(reindex)
print(series_reindex)
# 对Series对象重新设置索引,非交集设置为fill_value的值(本例中设置为0)
series_reindex_fill_value = series.reindex(reindex, fill_value=0)
print(series_reindex_fill_value)
# 向前填充
series_reindex_ffill = series.reindex(reindex, method="ffill")
print(series_reindex_ffill)
# 向后填充
series_reindex_bfill = series.reindex(reindex, method="bfill")
print(series_reindex_bfill)
series 返回结果:
i1 11
i2 22
i3 33
i4 44
i6 66
dtype: int64
series_reindex 返回结果:
i0 NaN
i1 11.0
i2 22.0
i3 33.0
i4 44.0
i5 NaN
i6 66.0
i7 NaN
dtype: float64
series_reindex_fill_value 返回结果:
i0 0
i1 11
i2 22
i3 33
i4 44
i5 0
i6 66
i7 0
dtype: int64
series_reindex_ffill 返回结果:
i0 NaN
i1 11.0
i2 22.0
i3 33.0
i4 44.0
i5 44.0
i6 66.0
i7 66.0
dtype: float64
series_reindex_bfill 返回结果:
i0 11.0
i1 11.0
i2 22.0
i3 33.0
i4 44.0
i5 66.0
i6 66.0
i7 NaN
dtype: float64