gpt4 book ai didi

python - "DataFrame"对象没有属性 'reshape'

转载 作者:太空狗 更新时间:2023-10-29 20:38:30 26 4
gpt4 key购买 nike

我想在没有标题的 CSV 文件中 reshape 一些数据,但我一直收到此错误

AttributeError: 'DataFrame' object has no attribute 'reshape'

这是我的脚本,我只想 reshape 第二列中的数据

import pandas as pd

df = pd.read_csv("test.csv", header=None, usecols=[1])

start = 0
for i in range(0, len(df.index)):
if (i + 1)%10 == 0:
result = df.iloc[start:i+1].reshape(2,5)
start = i + 1
print result

这是 CSV

1,52.1
2,32.2
3,44.6
3,99.1
5,12.3
3,43.2
7,79.4
8,45.5
9,56.3
0,15.4
1,35.7
2,23.7
3,66.7
4,33.8
1,12.9
7,34.8
1,21.6
3,43.7
6,44.2
9,55.8

输出应该是这样的

[[  52.1   32.2   44.6   99.1  12.3]
[ 43.2 79.4 45.5 56.3 15.4]]
[[ 35.7 23.7 66.7 33.8 12.9]
[ 34.8 21.6 43.7 44.2 55.8]]

有什么想法吗?谢谢

最佳答案

pandas.dataframe 没有内置的 reshape 方法,但您可以使用 .values 访问底层的 numpy 数组对象并对其调用 reshape:

start = 0
for i in range(0, len(df.index)):
if (i + 1)%10 == 0:
result = df.iloc[start:i+1].values.reshape(2,5)
start = i + 1
print result

#[[ 52.1 32.2 44.6 99.1 12.3]
# [ 43.2 79.4 45.5 56.3 15.4]]
#[[ 35.7 23.7 66.7 33.8 12.9]
# [ 34.8 21.6 43.7 44.2 55.8]]

关于python - "DataFrame"对象没有属性 'reshape',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com