ドキュメントストリングに記載されているコーディングクラスでこの問題が発生しています。インデックスをリセットしても次のエラーが表示される理由を説明しただけでなく、コードの最適化に関するヘルプに感謝します。フィルタと並べ替えを使用してPandasクエリで未解決のエラーが発生する
import pandas as pd
def beds_top_ten(df, facility_id):
'''
INPUT: DataFrame, int
OUTPUT: date
Write a pandas query that returns the ten census dates with the highest
number of available beds for the nursing home with the specified facility id
REQUIREMENTS:
Do a filter followed by a sort rather than a sort followed by a merge.
'''
df = pd.read_csv('beds.csv', low_memory= False)
df['Bed Census Date'] = pd.to_datetime(df['Bed Census Date'])
df = df.filter(items =['Facility ID', 'Bed Census Date','Available Residential Beds'])
df = df.sort_values(by =[ 'Facility ID', 'Available Residential Beds'], ascending= False)
df_group_by_ten = df.groupby('Facility ID').head(10).reset_index(drop=True)
dates = df_group_by_ten.loc[df_group_by_ten['Facility ID']==facility_id, 'Bed Census Date']
return dates
このテーブルは最初GROUPBY後に次のようになります。私は私のCOMMAND_LINEから実行したときに
Facility ID Bed Census Date Available Residential Beds
336 19 2011-01-05 29
339 19 2010-12-15 28
330 19 2011-02-23 27
332 19 2011-02-02 27
333 19 2011-01-26 27
334 19 2011-01-19 27
335 19 2011-01-12 27
338 19 2010-12-22 27
341 19 2010-12-01 27
331 19 2011-02-09 26
16 17 2013-04-10 22
87 17 2011-11-09 19
30 17 2013-01-02 17
37 17 2012-11-07 17
47 17 2012-08-29 17
31 17 2012-12-26 16
56 17 2012-06-20 16
10 17 2013-05-22 15
27 17 2013-01-23 15
61 17 2012-05-16 15
そして:
In [15]: beds_top_ten('beds.csv',17)
Out[15]:
16 2013-04-10
87 2011-11-09
30 2013-01-02
37 2012-11-07
47 2012-08-29
31 2012-12-26
56 2012-06-20
10 2013-05-22
27 2013-01-23
61 2012-05-16
Name: Bed Census Date, dtype: datetime64[ns]
しかし、私は同じコードを実行するとオンライン環境では、次のエラーが表示されます。
/usr/local/lib/python2.7/unittest/suite.py:108: DtypeWarning: Columns (10,45) have mixed types. Specify dtype option on import or set low_memory=False.
test(result)
E
======================================================================
ERROR: test_fourth_pandas (test_methods.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/test_methods.py", line 25, in test_fourth_pandas
all_equal = np.all(result == answer)
File "/usr/local/lib/python2.7/site-packages/pandas/core/ops.py", line 812, in wrapper
raise ValueError(msg)
ValueError: Can only compare identically-labeled Series objects
----------------------------------------------------------------------
Ran 1 test in 19.743s
FAILED (errors=1)
私はあなたがそれをoverthinkingていると思います。これは十分であるはずです: 'df [df ['Facility ID'] == facility_id] .sort_values( '利用可能な住宅ベッド'、昇順= False).head(10)' –
@COLDSPEED、ありがとう、この行は簡素化を助けましたしかし、私はまだ同じエラーを受けています。 – whd
下記の私の答えを参照してください。 –