"Dataframe" Object Has No Attribute 'Reshape'

"Dataframe" Object Has No Attribute 'Reshape'

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

Here is the 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

Output should be like this

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

Any ideas? Thank you

2

3 Answers

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

Simplest Answer

df = pd.read_csv("test.csv", header=None,usecols=[1])
df.values.reshape(-1,5)
    
array([[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]])
import pandas as pd
from sklearn.model_selection import train_test_split
df=pd.read_csv('diabetes1.csv')
df.head()
X=df.iloc[:,:-1]
Y=df.iloc[:,-1].values
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
Xnorm = pd.DataFrame(data = scaler.fit_transform(X))
Yshape = pd.DataFrame(data = Y.reshape(-1,1))
Xshape = pd.DataFrame(data=X.reshape(-1,1))
Ynorm = pd.DataFrame(data = scaler.fit_transform(Yshape))
XTrain,XTest,YTrain,YTest=train_test_split(Xnorm,Ynorm,train_size=0.7,random_state=2)
import keras.backend as K
def r2(y_true, y_pred):
    SS_res =  K.sum(K.square(y_true - y_pred)) 
    SS_tot = K.sum(K.square(y_true - K.mean(y_true))) 
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
import numpy as np
model1 = Sequential()
model1.add(Dense(100,activation='relu',input_dim=3))
model1.add(Dense(100,activation='relu'))
model1.add(Dense(1))
np.random.seed(50)
model1.compile(optimizer='adam',loss='mean_squared_error',metrics=['mse',r2])
early_stopping_monitor = EarlyStopping(monitor ="val_loss", 
                                        mode ="min", patience = 5, 
                                        restore_best_weights = True)
history=model1.fit(XTrain,YTrain,epochs=5000,
                  batch_size=64,validation_data=(XTest,YTest),
                  callbacks=[early_stopping_monitor]);

from sklearn.metrics import r2_score
y_train_pred = model1.predict(XTrain)
y_test_pred = model1.predict(XTest)
y_pred = model1.predict(Xnorm)
r2_score(YTrain,y_train_pred),r2_score(YTest,y_test_pred),r2_score(Ynorm,y_pred)

from sklearn.metrics import mean_absolute_error
mae=mean_absolute_error
mae(YTrain,y_train_pred),mae(YTest,y_test_pred),mae(Ynorm,y_pred)
print(mae(YTrain,y_train_pred),mae(YTest,y_test_pred),mae(Ynorm,y_pred))

from sklearn.metrics import mean_squared_error
mse=mean_squared_error
mse(YTrain,y_train_pred),mse(YTest,y_test_pred),mse(Ynorm,y_pred)
print(mse(YTrain,y_train_pred),mse(YTest,y_test_pred),mse(Ynorm,y_pred))

I have this code and want to run it but get this error: AttributeError: 'DataFrame' object has no attribute 'reshape'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.