2014-10-10 53 views
0

我想在Python 2.7排序如下熊貓數據框:熊貓數據幀從NumPy的陣列 - 不正確的數據類型和無法改變

import numpy as np 
import pandas as pd 

heading_cols = ["Video Title", "Up Ratings", "Down Ratings", "Views", "User Name","Subscribers"] 
column_1 = ["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"] 
column_2 = [1295, 5905, 112, 1357, 2058, 1566, 5386] 
column_3 = [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769] 
column_4 = [600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4] 
column_5 = ["Bob","Tom","Dave","Sally","Rick","Mary","Roberta"] 
column_6 = [25000,30000,15000,15005,20000,31111,11000] 

#Generate data: 
xdata_arr = np.array([column_1,column_2,column_3,column_4,column_5,column_6]).T 

# Generate the DataFrame: 
df = pd.DataFrame(xdata_arr, columns=heading_cols) 
print df 

接下來的兩行代碼導致問題:

# Print DataFrame and basic stats: 
print df["Up Ratings"].describe() 
print df.sort('Views', ascending=False) 

問題:

  • 排序是不工作的任何列。
  • 統計信息應該包括諸如平均值,標準差,最小值,最大值等,這些值不顯示。

問題是,dtypes()返回所有列的「對象」。這是錯誤的。有些應該是整數,但我不知道如何只改變數字。我曾嘗試過:

df.convert_objects(convert_numeric=True) 

但這不起作用。所以,然後我去了NumPy陣列,試圖改變那裏的dtypes:

dt = np.dtype([(heading_cols[0], np.str_), (heading_cols[1], np.int16), (heading_cols[2], np.int16), (heading_cols[3], np.int16), (heading_cols[4], np.str_), (heading_cols[5], np.int16) ]) 

但是這也不起作用。

有沒有辦法將dtype手動更改爲數字?

+2

''convert_object()''最喜歡的大熊貓方法返回一個新的對象,這樣做:''DF = df.convert_object(convert_numeric =真)'' – Jeff 2014-10-10 14:40:03

+0

好吧,我只是去嘗試,但我正在逐漸此消息:Traceback(最近調用最後一次): 文件「C:\ Python27 \ testing.py」,第23行,在 df = pd.DataFrame(xdata_arr,columns = heading_cols).convert_object(convert_numeric = True) 文件「C:\ python27 \ lib \ site-packages \ pandas \ core \ generic.py」,第1843行,在__getattr__ (type(self).__ name__,name)) AttributeError:'DataFrame'object has no attribute' convert_object' – 2014-10-10 14:47:33

+1

錯字:''convert_objects'' – Jeff 2014-10-10 14:48:26

回答

1

像大多數熊貓方法一樣,convert_objects返回一個NEW對象。

In [20]: df.convert_objects(convert_numeric=True) 
Out[20]: 
    Video Title Up Ratings Down Ratings Views User Name Subscribers 
0 Adelaide  1295  1158259 600.5  Bob  25000 
1 Brisbane  5905  1857594 1146.4  Tom  30000 
2  Darwin   112  120900 1714.7  Dave  15000 
3  Hobart  1357  205556 619.5  Sally  15005 
4  Sydney  2058  4336374 1214.8  Rick  20000 
5 Melbourne  1566  3806092 646.9  Mary  31111 
6  Perth  5386  1554769 869.4 Roberta  11000 

In [21]: df.convert_objects(convert_numeric=True).dtypes 
Out[21]: 
Video Title  object 
Up Ratings  int64 
Down Ratings  int64 
Views   float64 
User Name  object 
Subscribers  int64 
dtype: object 
+0

非常感謝。正如你所做的那樣,我可以在一行中完成整個事情 - 創建數據框並同時進行轉換。更重要的是,我並不知道「返回新對象」部分。爲了繞過這個,爲什麼我不能在這裏使用「inplace = True」:df.convert_objects(convert_numeric = True,inplace = True)? <---我試過這個,我得到一個錯誤信息。 – 2014-10-10 15:01:38

+1

yeh,''convert_objects''沒有一個inplace,我是一個很大的-1,通常不會使用它們。它們對讀者來說不是直覺,並且很少提供性能優勢。 – Jeff 2014-10-10 15:04:02

+0

非常感謝。這裏所有的問題都回答了。 – 2014-10-10 15:32:00