如何重塑Pandas系列
在这篇文章中,我们将看到如何重塑Pandas系列。因此,为了重塑Pandas系列,我们使用Pandas系列对象的reshape()方法。
语法: Pandas.Series.values.reshape((dimension))
返回:返回一个带有shape值的ndarray,如果指定的shape与当前shape完全匹配,则返回self(为了兼容)。
让我们看看其中的一些例子。
示例 1:
# import pandas library
import pandas as pd
# make an array
array = [2, 4, 6, 8, 10, 12]
# create a series
series_obj = pd.Series(array)
# convert series object into array
arr = series_obj.values
# reshaping series
reshaped_arr = arr.reshape((3, 2))
# show
reshaped_arr
输出:
示例 2:
# import pandas library
import pandas as pd
# make an array
array = ["ankit","shaurya",
"shivangi", "priya",
"jeet","ananya"]
# create a series
series_obj = pd.Series(array)
print("Given Series:\n", series_obj)
# convert series object into array
arr = series_obj.values
# reshaping series
reshaped_arr = arr.reshape((2, 3))
# show
print("After Reshaping: \n", reshaped_arr)
输出: