【发布时间】:2020-10-08 17:35:55
【问题描述】:
我在 DataFrame 中加载一个 csv 并获取一些 NaN 值。
我想用我拥有的自定义函数计算和替换这些 NaN 值。
示例代码:
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 8 20:44:27 2020
@author: theo
"""
import pandas as pd
import math
def customfunction(arg1):
arg2 = arg1 * arg1
arg3 = arg2 + arg1
return arg1, arg2, arg3
dt = pd.DataFrame([[0, 5, math.nan], [-5, 2, 3], [3, -7, 4]])
for index, row in dt.iterrows():
if (row.isnull().values.any()):
(_, arg2, arg3) = customfunction(arg1=row[0]) # Yes the row that contains the NaN values also has the arg1 value I need to compute the rest values
dt.loc[index,1] = arg2
dt.loc[index,2] = arg3
上面的代码可以工作......但是有人可以提出更好的建议吗?
我正在使用建议的方法(慢 2 倍)发布一个真实案例的时间比较 我想指出,在实际情况下,这些值是从表中获取的,而不是简单地计算出来的。因此,请为您的示例使用返回多个值的函数定义。
start_time = time.time()
daily1 = daily.apply(lambda x: pd.Series(np.where(any(x.isna()), (getdaytempartures(date=x[0],ht=hourly)), (x[0], x[1], x[2], x[3]))), axis=1)
print("--- %s seconds ---" % (time.time() - start_time))
--- 252.25447249412537 seconds ---
start_time = time.time()
for index, row in daily.iterrows():
if (row.isnull().values.any()):
(_, tavg, tmin, tmax) = getdaytempartures(date=row['date'], ht=hourly)
daily.loc[index,'tavg'] = tavg
daily.loc[index,'tmin'] = tmin
daily.loc[index,'tmax'] = tmax
print("--- %s seconds ---" % (time.time() - start_time))
--- 113.31336617469788 seconds ---
start_time = time.time()
#for key in daily.keys():
daily3 = daily.apply(cf, ht=hourly, axis=1)
print("--- %s seconds ---" % (time.time() - start_time))
--- 108.97707056999207 seconds ---
更多细节。 daily 有以下列:names=['date', 'tavg', 'tmin', 'tmax'] 并且 hourly 有以下列:names=['date', 'time', 'temp']
返回一行的示例计算函数是:
def cf(row, ht):
if row.isnull().values.any():
dt = ht.loc[ht['date'] == row[0]].dropna()
row['tmax'] = dt['temp'].max()
row['tmin'] = dt['temp'].min()
row['tavg'] = dt['temp'].sum() / dt['temp'].count()
return row
前30个样本数据是每个表的前30个......那些不会帮助tbh:
daily:
,date,tavg,tmin,tmax
0,1963-01-03,27.3,16.1,33.9
1,1963-01-04,27.3,16.1,33.9
2,1963-01-05,26.7,17.8,35.0
3,1963-01-06,26.7,17.8,33.9
4,1963-01-07,27.6,17.2,33.9
5,1963-01-08,26.9,17.8,33.9
6,1963-01-09,27.3,18.9,33.9
7,1963-01-10,26.8,20.0,35.0
8,1963-01-13,27.3,17.8,33.9
9,1963-01-14,27.2,17.8,33.9
10,1963-01-15,27.9,17.8,35.0
11,1963-01-16,27.5,17.8,35.0
12,1963-01-17,27.5,17.8,36.1
13,1963-01-18,27.6,17.8,33.9
14,1963-01-19,26.9,17.8,35.0
15,1963-01-20,27.3,18.9,35.0
16,1963-01-21,27.6,17.8,35.0
17,1963-01-22,26.0,17.8,35.0
18,1963-01-23,28.1,17.8,33.9
19,1963-01-24,27.6,18.9,32.8
20,1963-01-25,28.3,17.8,33.9
21,1963-01-26,28.1,17.8,35.0
22,1963-01-27,28.5,17.8,35.0
23,1963-01-28,27.7,17.8,36.1
24,1963-01-29,27.9,17.2,35.0
25,1963-01-30,28.1,17.2,37.2
26,1963-02-05,26.1,18.9,33.9
27,1963-02-11,29.2,17.8,33.9
28,1963-02-12,29.3,18.9,36.1
29,1963-02-13,29.7,18.9,36.1
hourly:
,date,time,temp
0,1957-07-01,0,25.0
1,1957-07-01,12,22.2
2,1957-07-01,18,27.2
3,1957-07-02,0,26.1
4,1957-07-02,12,22.2
5,1957-07-02,18,27.8
6,1957-07-03,0,26.1
7,1957-07-03,12,22.2
8,1957-07-03,18,28.9
9,1957-07-04,0,25.0
10,1957-07-04,12,22.2
11,1957-07-04,18,28.9
12,1957-07-05,0,25.0
13,1957-07-05,12,21.1
14,1957-07-05,18,25.0
15,1957-07-06,0,25.0
16,1957-07-06,12,20.0
17,1957-07-06,18,27.8
18,1957-07-07,0,25.0
19,1957-07-07,12,21.1
20,1957-07-07,18,27.8
21,1957-07-08,0,25.0
22,1957-07-08,12,21.1
23,1957-07-08,18,28.9
24,1957-07-09,0,23.9
25,1957-07-09,12,20.0
26,1957-07-09,18,25.0
27,1957-07-10,0,23.9
28,1957-07-10,12,17.8
29,1957-07-10,18,26.1
Hourly 1977-02-20: this is a 1 day example that I used to debug
,date,time,temp
36493,1977-02-20,0,27.0
36494,1977-02-20,1,26.0
36495,1977-02-20,2,26.0
36496,1977-02-20,3,26.0
36497,1977-02-20,11,23.0
36498,1977-02-20,12,23.0
36499,1977-02-20,13,
36500,1977-02-20,14,27.0
36501,1977-02-20,15,29.0
36502,1977-02-20,16,
36503,1977-02-20,17,30.0
36504,1977-02-20,18,32.0
36505,1977-02-20,19,33.0
36506,1977-02-20,20,33.0
36507,1977-02-20,21,32.0
36508,1977-02-20,22,30.0
36509,1977-02-20,23,28.0
daily:
,date,tavg,tmin,tmax
3297,1977-02-20,28.3,,34.0
gl 和 hf...我认为没有数据更容易解决...
谢谢
【问题讨论】:
标签: python pandas dataframe nan