vertically integrate a datasheet with a over 1500 columns

105 views Asked by At

I have a data sheet with about 1700 columns and 100 rows of data w/ a unique identifier. It is survey data and every employee of an organization answer the same 9 questions but its compiled into one row of data for every organization. Is there a way in python/pandas to vertically integrate this data as opposed to the elongated format on the x-axis it already is at? I am cutting and pasting currently.

1

There are 1 answers

1
Quang Hoang On

You can reshape the underlying numpy array and reindex with proper companies:

# sample data, assuming index is the company
df = pd.DataFrame(np.arange(36).reshape(2,-1))

# new index
idx = df.index.repeat(df.shape[1]//9)

# new data:
new_df = pd.DataFrame(df.values.reshape(-1,9), index=idx)

Output:

    0   1   2   3   4   5   6   7   8
0   0   1   2   3   4   5   6   7   8
0   9  10  11  12  13  14  15  16  17
1  18  19  20  21  22  23  24  25  26
1  27  28  29  30  31  32  33  34  35