lreshape

reshape a pandas dataframe

寵の児 提交于 2019-12-17 16:47:09
问题 suppose a dataframe like this one: df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]], columns = ['A', 'B', 'A1', 'B1']) I would like to have a dataframe which looks like: what does not work: new_rows = int(df.shape[1]/2) * df.shape[0] new_cols = 2 df.values.reshape(new_rows, new_cols, order='F') of course I could loop over the data and make a new list of list but there must be a better way. Any ideas ? 回答1: The pd.wide_to_long function is built almost exactly for this situation, where you

reshape a pandas dataframe

本小妞迷上赌 提交于 2019-11-29 09:50:27
suppose a dataframe like this one: df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]], columns = ['A', 'B', 'A1', 'B1']) I would like to have a dataframe which looks like: what does not work: new_rows = int(df.shape[1]/2) * df.shape[0] new_cols = 2 df.values.reshape(new_rows, new_cols, order='F') of course I could loop over the data and make a new list of list but there must be a better way. Any ideas ? The pd.wide_to_long function is built almost exactly for this situation, where you have many of the same variable prefixes that end in a different digit suffix. The only difference here is