2016-12-20 3 views
1

データをきれいに印刷するために、私は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   | 
+----+---------------+------------+----------+-----------------------------------------------------+--------+-------+---------+---------+------------------+---------+---------+------------+ 
+0

を使用して行うことができnumpy.reshape

surv_age = np.reshape(surv_age, (-1, 1)) 

を使用して簡単に行うことができますか? – martianwars

+0

https://github.com/gregbanks/python-tabulate –

+0

申し訳ありませんが、私は図書館のコードを読んでいません。私はあなたに確認して教えます。 –

答えて

1

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)形状の1-D numpyの配列されています。ここでは何ができるかTABULATEのサンプルです。 2-D numpy配列に再整形する必要があります。あなたはまた、これはあなたが `tabulateの()`のコードを追加することができ、このようなnp.expand_dims

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

ありがとうございました。印刷する配列のサイズを制限するにはどうすればよいですか?私はsurv_age [:20] [0]を使ってみましたが、 'numpy.float64'にはlen()がありませんというエラーが表示されます –

+0

' surv_age [:20、:] 'はどうですか? – martianwars

+1

これもうまくいきました –

関連する問題