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]]
Answer from akuiper on Stack Overflowpandas.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]]
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]])
AttributeError: 'DataFrame' object has no attribute 'reshape'
BUG: Series has no attribute "reshape" after adding a new category in df
Seeing AttributeError: 'Dataset' object has no attribute 'reshape' when using "dataset.get_nearest_examples"
python 3.x - DataFrame has no Reshape Attribute - Stack Overflow
Solution was linked on reshaped method on documentation page.
Insted of Y.reshape(-1,1) you need to use:
Y.values.reshape(-1,1)
The solution is indeed to do:
Y.values.reshape(-1,1)
This extracts a numpy array with the values of your pandas Series object and then reshapes it to a 2D array.
The reason you need to do this is that pandas Series objects are by design one dimensional. Another solution if you would like to stay within the pandas library would be to convert the Series to a DataFrame which would then be 2D:
Y = pd.Series([1,2,3,1,2,3,4,32,2,3,42,3])
scaler = StandardScaler()
Ys = scaler.fit_transform(pd.DataFrame(Y))