データをきれいに印刷するために、私はtabulate
ライブラリをPythonで使用しています。 ここで私が使用しているコードは次のとおりです。tabulate libをfloat64で使用する方法:python
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からチタンデータセットです。今、私は浮動小数点値を持つデータにtabulateを使用する必要があります。ここで私にエラーを与えているコードは次のとおりです。
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
でそれが不可能な場合は、そうすることができるかわいい印刷の別の方法を提案してください。 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 |
+----+---------------+------------+----------+-----------------------------------------------------+--------+-------+---------+---------+------------------+---------+---------+------------+
を使用して行うことができ
numpy.reshape
、を使用して簡単に行うことができますか? – martianwars
https://github.com/gregbanks/python-tabulate –
申し訳ありませんが、私は図書館のコードを読んでいません。私はあなたに確認して教えます。 –