[英]Use function (not using lambda) with apply method for pandas DataFrame
[英]Error when trying to apply lambda function using pandas Dataframe
我有一個帶有日期時間索引的數據框,看起來像這樣:
ModelRun Tmp_2m_C DSWRF TCDC Obs_kW n beta \
2016-01-01 06:30:00 2.016010e+09 7.962387 0.00000 100.0 0.0 1 0.0
2016-01-01 07:30:00 2.016010e+09 8.077713 9.00000 100.0 0.0 1 0.0
2016-01-01 08:30:00 2.016010e+09 8.467117 46.32202 100.0 12.0 1 0.0
delta dtm_utc \
2016-01-01 06:30:00 -23.058629 2016-01-01 06:30:00+00:00
2016-01-01 07:30:00 -23.058629 2016-01-01 07:30:00+00:00
2016-01-01 08:30:00 -23.058629 2016-01-01 08:30:00+00:00
dtm_local ... \
2016-01-01 06:30:00 2016-01-01 07:30:00+01:00 ...
2016-01-01 07:30:00 2016-01-01 08:30:00+01:00 ...
2016-01-01 08:30:00 2016-01-01 09:30:00+01:00 ...
corr1_dtm dtm_sun \
2016-01-01 06:30:00 -1 days +23:45:13.666667 2016-01-01 07:12:19.401323+01:00
2016-01-01 07:30:00 -1 days +23:45:13.666667 2016-01-01 08:12:19.401323+01:00
2016-01-01 08:30:00 -1 days +23:45:13.666667 2016-01-01 09:12:19.401323+01:00
sun_hour sun_hour_angle delta_rad sun_hour_angle_rad \
2016-01-01 06:30:00 7.2 -72.0 -0.402449 -1.256637
2016-01-01 07:30:00 8.2 -57.0 -0.402449 -0.994838
2016-01-01 08:30:00 9.2 -42.0 -0.402449 -0.733038
earth_sunset_deg earth_sunrise_deg surface_sunset_deg \
2016-01-01 06:30:00 68.645391 -68.645391 70.481456
2016-01-01 07:30:00 68.645391 -68.645391 70.481456
2016-01-01 08:30:00 68.645391 -68.645391 70.481456
surface_sunrise_deg
2016-01-01 06:30:00 -79.585047
2016-01-01 07:30:00 -79.585047
2016-01-01 08:30:00 -79.585047
請注意,我已經放置了所有數據框列,以便您可以嘗試追溯錯誤,但是在我嘗試執行的操作中,我僅對最后四列感興趣,因此在數據框的這一部分中:
earth_sunset_deg earth_sunrise_deg surface_sunset_deg \
2016-01-01 06:30:00 68.645391 -68.645391 70.481456
2016-01-01 07:30:00 68.645391 -68.645391 70.481456
2016-01-01 08:30:00 68.645391 -68.645391 70.481456
surface_sunrise_deg
2016-01-01 06:30:00 -79.585047
2016-01-01 07:30:00 -79.585047
2016-01-01 08:30:00 -79.585047
這只是數據框的一部分,因為它包含2年的數據。 我正在嘗試做以下事情:
if surface_sunset_deg > earth_sunset_deg:
sunset_deg = earth_sunset_deg
else:
sunset_deg = surface_sunset_deg
因此,從本質surface_sunset_deg or earth_sunset_deg
,我試圖遍歷數據幀的所有行(對應於不同的時間戳),評估2個角度中的哪個更大( surface_sunset_deg or earth_sunset_deg
),並將滿足我的條件的角度存儲在新列df["sunset_deg"]
。
據我所知,遍歷數據幀的最有效方法是使用apply
函數,因此,我寫的是:
df["sunset_deg"] = df.apply(lambda row: row["earth_sunset_deg"] if row["earth_sunset_deg"] < row["surface_sunset_deg"] else row["surface_sunset_earth"], axis=1)
我得到的錯誤是這樣的:
Traceback (most recent call last):
File "C:\Users\Admin\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2483, in get_value
return libts.get_value_box(s, key)
File "pandas/_libs/tslib.pyx", line 923, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18843)
File "pandas/_libs/tslib.pyx", line 932, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18477)
TypeError: 'str' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Admin\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-11-69be989aa737>", line 1, in <module>
df.apply(lambda row: row["earth_sunset_deg"] if row["earth_sunset_deg"] < row["surface_sunset_deg"] else row["surface_sunset_earth"], axis=1)
File "C:\Users\Admin\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4262, in apply
ignore_failures=ignore_failures)
File "C:\Users\Admin\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4358, in _apply_standard
results[i] = func(v)
File "<ipython-input-11-69be989aa737>", line 1, in <lambda>
df.apply(lambda row: row["earth_sunset_deg"] if row["earth_sunset_deg"] < row["surface_sunset_deg"] else row["surface_sunset_earth"], axis=1)
File "C:\Users\Admin\Anaconda3\lib\site-packages\pandas\core\series.py", line 601, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\Admin\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2491, in get_value
raise e1
File "C:\Users\Admin\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2477, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('surface_sunset_earth', 'occurred at index 2016-02-02 00:30:00')
當我為數據幀的前30個元素運行同一行代碼時,則:
df["sunset_deg"] = df[:30].apply(lambda row: row["earth_sunset_deg"] if row["earth_sunset_deg"] < row["surface_sunset_deg"] else row["surface_sunset_earth"], axis=1)
運行順利,並產生了我想要的結果。 您能幫我追溯錯誤嗎? 我是Python的新手,在這里我已經盡力了,沒有成功。 先感謝您。
為此使用apply()
根本沒有效率。 除非萬不得已,否則幾乎不要使用apply()
。 您可以更簡單地解決問題:
df["sunset_deg"] = df[["earth_sunset_deg", "surface_sunset_deg"]].min(1)
這是一個替代方案,可能更容易擴展到不同條件:
df["sunset_deg"] = df["earth_sunset_deg"].where(df["surface_sunset_deg"] > df["earth_sunset_deg"], df["surface_sunset_deg"])
這些方法中的任何一個都比使用apply()
任何方法效率更高(這實際上只是一個for
循環,實在太慢了)。
問題在於指定的行中不存在“ surface_sunset_earth”。 確切地說,問題出在這里:
else row["surface_sunset_earth"]
如果指定行中不存在鍵“ surface_sunset_earth”,則無法獲取。
也許您不想在這里使用lambda。 對於較小的邏輯,lambda更好,當邏輯變大時,最好改用函數。
那將是一個解決方案:
def my_func(row):
try:
if row["earth_sunset_deg"] < row["surface_sunset_deg"]:
return row["earth_sunset_deg"]
else:
return row["surface_sunset_earth"]
except KeyError:
# Decide here what to do in case one of the keys aren't exists
pass
df["sunset_deg"] = df[:30].apply(my_func, axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.