使用 Python 将数组列表转换为可读 table

Conversion of arrays list into readable table with Python

我 运行 一些 ARIMA 拟合 Python 3 我想将 Ljung-Box 测试的结果保存到一个文本文件中(或保存到一个稍后要写入的对象中一个文件),但测试的输出远非可读性。

函数的一个例子是:

from statsmodels.stats import diagnostic as dst
ljung = dst.acorr_ljungbox(db['FTSEMIB'], lags=10, boxpierce=True)

输出如下所示:

(array([a, b, c]),array([1, 2, 3]),array([d, e, f]),array([4, 5, 6]))

我想在最终输出文件中得到的是这样的:

a 1
b 2
c 3

d 4
e 5
f 6

这是使用 numpy 的一种方法:

import pandas as pd
import numpy as np

x = (np.array(['a', 'b', 'c']),
     np.array([1, 2, 3]),
     np.array(['d', 'e', 'f']),
     np.array([4, 5, 6]))

A = np.array(x)

df = pd.DataFrame({0: A[::2].ravel(), 1: A[1::2].ravel()},
                  index=range(int(A.shape[1]*A.shape[0]/2)))

print(df)

   0  1
0  a  1
1  b  2
2  c  3
3  d  4
4  e  5
5  f  6