如何将制表库与 float64 一起使用:python

How to use tabulate lib with float64 : python

为了漂亮地打印数据,我在 python 中使用 tabulate 库。 这是我正在使用的代码:

train = pd.read_csv('../misc/data/train.csv')
test = pd.read_csv('../misc/data/test.csv')

# Prints the head of data prettily :)
print(tabulate(train.head(), headers='keys', tablefmt='psql'))

数据来自kaggle的titanic数据集。现在,我需要对具有 float64 值的数据使用制表。这是给我错误的代码:

surv_age = train[train['Survived'] == 1]['Age'].value_counts()
dead_age = train[train['Survived'] == 0]['Age'].value_counts()

print(tabulate(surv_age, headers='keys', tablefmt='psql'))

df = pd.DataFrame([surv_age, dead_age])
df.index = ['Survived', 'Dead']
df.plot(kind='hist', stacked=True, figsize=(15, 8))
plt.xlabel('Age')
plt.ylabel('Number of passengers')
plt.show()

错误是: 回溯(最近调用最后):

  File "main.py", line 49, in <module>
    print(tabulate(surv_age, headers='keys', tablefmt='psql'))
  File "/usr/local/lib/python2.7/dist-packages/tabulate.py", line 1109, in tabulate
    tabular_data, headers, showindex=showindex)
  File "/usr/local/lib/python2.7/dist-packages/tabulate.py", line 741, in _normalize_tabular_data
    rows = [list(row) for row in vals]
TypeError: 'numpy.float64' object is not iterable

第 49 行是代码中的 print(tabulate(.. 行。

如何迭代数据的 float64 值,以便我可以漂亮地打印在表格中?如果它在 tabulate 中不可能,请建议一种可以做到这一点的漂亮打印的替代方法。以下是制表功能的示例:

+----+---------------+------------+----------+-----------------------------------------------------+--------+-------+---------+---------+------------------+---------+---------+------------+
|    |   PassengerId |   Survived |   Pclass | Name                                                | Sex    |   Age |   SibSp |   Parch | Ticket           |    Fare | Cabin   | Embarked   |
|----+---------------+------------+----------+-----------------------------------------------------+--------+-------+---------+---------+------------------+---------+---------+------------|
|  0 |             1 |          0 |        3 | Braund, Mr. Owen Harris                             | male   |    22 |       1 |       0 | A/5 21171        |  7.25   | nan     | S          |
|  1 |             2 |          1 |        1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female |    38 |       1 |       0 | PC 17599         | 71.2833 | C85     | C          |
|  2 |             3 |          1 |        3 | Heikkinen, Miss. Laina                              | female |    26 |       0 |       0 | STON/O2. 3101282 |  7.925  | nan     | S          |
|  3 |             4 |          1 |        1 | Futrelle, Mrs. Jacques Heath (Lily May Peel)        | female |    35 |       1 |       0 | 113803           | 53.1    | C123    | S          |
|  4 |             5 |          0 |        3 | Allen, Mr. William Henry                            | male   |    35 |       0 |       0 | 373450           |  8.05   | nan     | S          |
+----+---------------+------------+----------+-----------------------------------------------------+--------+-------+---------+---------+------------------+---------+---------+------------+

引用 tabulate 文档,

The following tabular data types are supported:

  • list of lists or another iterable of iterables
  • list or another iterable of dicts (keys as columns)
  • dict of iterables (keys as columns)
  • two-dimensional NumPy array
  • NumPy record arrays (names as columns)
  • pandas.DataFrame

您的变量 surv_age 是形状为 (342,) 的一维 numpy 数组。您将需要重新整形为二维 numpy 数组。您可以使用 numpy.reshape

轻松完成此操作
surv_age = np.reshape(surv_age, (-1, 1))

您也可以使用 np.expand_dims 这样做,

surv_age = np.expand_dims(surv_age, axis=1)