python - pandas 将多键值数据帧列 reshape 为行

标签 python pandas reshape

我有一个像这样的数据框: enter image description here

如何将列 (0 => 1, 2=> 3) reshape 为记录存储? IE。列 metric_namemetric_value 以及多行(观察)?

pandas_dict = {0: {0: 'Model:',
  1: 'Dependent Variable:',
  2: 'Date:',
  3: 'No. Observations:',
  4: 'Df Model:',
  5: 'Df Residuals:',
  6: 'Converged:',
  7: 'No. Iterations:'},
 1: {0: 'Logit',
  1: 'sick_percentage',
  2: '2019-08-14 13:32',
  3: '28',
  4: '2',
  5: '25',
  6: '0.0000',
  7: '35.0000'},
 2: {0: 'Pseudo R-squared:',
  1: 'AIC:',
  2: 'BIC:',
  3: 'Log-Likelihood:',
  4: 'LL-Null:',
  5: 'LLR p-value:',
  6: 'Scale:',
  7: ''},
 3: {0: 'inf',
  1: '6.0798',
  2: '10.0764',
  3: '-0.039902',
  4: '0.0000',
  5: '1.0000',
  6: '1.0000',
  7: ''}}
df = pd.DataFrame(pandas_dict)

最佳答案

如果只有 4 列,您可以展平值并通过构造函数创建 DataFrame:

a = df[[0, 2]].values.ravel()
b = df[[1, 3]].values.ravel()

df = pd.DataFrame({'A':a, 'B':b})
print (df)
                      A                 B
0                Model:             Logit
1     Pseudo R-squared:               inf
2   Dependent Variable:   sick_percentage
3                  AIC:            6.0798
4                 Date:  2019-08-14 13:32
5                  BIC:           10.0764
6     No. Observations:                28
7       Log-Likelihood:         -0.039902
8             Df Model:                 2
9              LL-Null:            0.0000
10        Df Residuals:                25
11         LLR p-value:            1.0000
12           Converged:            0.0000
13               Scale:            1.0000
14      No. Iterations:           35.0000
15                                       

或一般解决方案 - 在具有模数和整数除法的列中创建 MultiIndex 并按 DataFrame.stack reshape :

df.columns = [df.columns % 2, df.columns // 2]
df = df.stack().reset_index(drop=True)
print (df)
                      0                 1
0                Model:             Logit
1     Pseudo R-squared:               inf
2   Dependent Variable:   sick_percentage
3                  AIC:            6.0798
4                 Date:  2019-08-14 13:32
5                  BIC:           10.0764
6     No. Observations:                28
7       Log-Likelihood:         -0.039902
8             Df Model:                 2
9              LL-Null:            0.0000
10        Df Residuals:                25
11         LLR p-value:            1.0000
12           Converged:            0.0000
13               Scale:            1.0000
14      No. Iterations:           35.0000
15                                       

关于python - pandas 将多键值数据帧列 reshape 为行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57494188/

相关文章:

python - Pandas : finding out wrong data

熔化( reshape )中的 R 错误 - 行数

python - 抓取Python源文件中不可见的数据

python - 编写函数,如果可以通过在列表中进行加法或减法运算来获得目标整数,则应返回 true

用于生成正则表达式的 Python 库

python - DoesNotExist 在/admin/login/站点匹配查询不存在

python - 加快从列表创建 numpy 数组

python-2.7 - Python pandas dataframe 警告,建议改用 .loc?

r - 如何使用 reshape/plyr 汇总数据?

python - 根据默认大小 reshape np.array