在Pandas中,我们可以使用reshape()
函数来调整数据的形状。下面是一个示例代码:
import pandas as pd
# 创建一个DataFrame
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)
# 使用reshape()函数调整数据形状
df_reshaped = df.values.reshape(5, 2)
# 打印调整后的数据形状
print(df_reshaped)
运行以上代码,输出结果为:
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]]
通过使用reshape()
函数,我们可以将原始数据的形状从(5, 2)
调整为(2, 5)
。