"DataFrame";对象没有属性“重塑" [英] "DataFrame" object has no attribute 'reshape'

查看:50
本文介绍了"DataFrame";对象没有属性“重塑"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不包含标题的CSV文件中重塑一些数据,但我不断收到此错误

I want to reshape some data in a CSV file without header but I keep getting this error

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

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

This is my script, I want to reshape the data in 2nd column only

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 :

pandas.dataframe doesn't have a built-in reshape method, but you can use .values to access the underlying numpy array object and call reshape on it:

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]]

这篇关于"DataFrame";对象没有属性“重塑"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆