如何将列 (0 => 1, 2=> 3) reshape 为记录存储?
IE。列 metric_name
和 metric_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/