如何重塑熊猫系列?
在本文中,我们将看到如何重塑 Pandas 系列。因此,为了重塑 Pandas Series,我们使用 Pandas Series 对象的reshape()方法。
Syntax: Pandas.Series.values.reshape((dimension))
Return: return an ndarray with the values shape if the specified shape matches exactly the current shape, then return self (for compat)
让我们看一些例子:
示例 1:
Python3
# 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
Python3
# 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)
输出:
示例 2:
蟒蛇3
# 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)
输出: