我在数据帧中加载一个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有以下列:name=['date','tavg','tmin','tmax'] 每小时有以下列:name=['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个。。。这些对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。。。我认为没有数据的情况下更容易解决
多谢各位
min
、max
和mean
李>'hourly'
数据帧按'date'
分组,然后为'temp'
聚合min
、mean
和max
,创建hg
。hg
可以用于{a1}{daily = daily.update(hg)
不正确)李>overwrite=True
将更新数据帧中的所有值,而不仅仅是NaN
值。overwrite=False
仅用于更新数据帧的NaN
值NaN
子设置所有行,并使用overwrite=True
李>设置数据帧
daily
显示缺少的值hg
用指标显示每日分组仅更新
NaN
值如果存在
NaN
,则更新整行样本数据
假设您要在以下数据框中替换NAN:
df = pd.DataFrame({"col1": [None, np.nan, 1.0], "col2": [1,2,3]})
可以对整个列而不是行中的每个元素使用操作:
它给你:
col3和col4相当于示例中的arg2和arg3
df_.isnull().any(axis=1)
将为所有行提供至少一个NaN另一个解决方案
相关问题 更多 >
编程相关推荐