Using Python Generate Array 18 Odd Numbers Starting 1 Reshape Make 3×6 Matrix Convert Data Q37023001

Using Python Generate an array of 18 odd numbers starting from1. Reshape it to make 3×6 matrix and convert it into data frame.Swap the even number columns with odd number columns and print theresult.


Solution


Code:

import numpy as npimport pandas as pdV = []for i in range(18): V.append(2*i + 1)M = np.asarray(V).reshape((3, 6))df = pd.DataFrame(M)print(df)t = df.copy()for i in range(6): if(i % 2 == 0): df[:][i] = t[:][i + 1] else: df[:][i] = t[:][i – 1]

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.