내가 파이썬 2.7에서 다음 팬더 DataFrame을 정렬하는 데 노력하고 변경할 수 없습니다 : 코드의팬더 Dataframe 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
다음 2 줄 일으키는 문제 :
# Print DataFrame and basic stats:
print df["Up Ratings"].describe()
print df.sort('Views', ascending=False)
을
문제 :
- 정렬은 열에 대해 작동하지 않습니다.
- 통계에 mean, std, min, max 등의 정보가 포함되어야합니다. 이러한 정보는 표시되지 않습니다.
문제는 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을 숫자로 변경하는 방법이 있습니까?
''convert_object()''가장 팬더 방법과 같은 새로운 객체를 반환 할 :''DF = df.convert_object (convert_numeric = TRUE)는'' – Jeff
가 좋아, 그냥 시도,하지만 난 무엇입니까 이 메시지는 다음과 같습니다. Traceback (가장 최근에 마지막으로 호출) : 파일 "C : \ Python27 \ testing.py" df = pd.DataFrame (xdata_arr, columns = heading_cols) .convert_object (convert_numeric = True) 파일 형식 : C : \ Python27 \ lib \ site-packages \ pandas \ core \ generic.py, 1843 줄, __getattr__ (유형 (자체) .__ name__, 이름) AttributeError : 'DataFrame' convert_object ' –
오타 :''convert_objects'' – Jeff