Python 用其他行的数据填充空值
我有一个有许多空值的数据帧。我想用同一用户的数据填充空值,这些数据来自同一列,但在以后的日期。以下是数据帧:Python 用其他行的数据填充空值,python,pandas,Python,Pandas,我有一个有许多空值的数据帧。我想用同一用户的数据填充空值,这些数据来自同一列,但在以后的日期。以下是数据帧: import pandas as pd import numpy as np array = {'user': ['Trevor', 'John', 'Trevor', 'John', 'Trevor', 'Trevor', 'John'], 'date': ['2020-10-11 08:00:00', '2020-10-15 08:00:00', '2020-10-17 08:00:
import pandas as pd
import numpy as np
array = {'user': ['Trevor', 'John', 'Trevor', 'John', 'Trevor', 'Trevor', 'John'], 'date': ['2020-10-11 08:00:00', '2020-10-15 08:00:00', '2020-10-17 08:00:00', '2020-10-19 08:00:00', '2020-10-10 08:00:00'
, '2020-11-11 12:34:00', '2020-11-16 09:12:00'], 'test1': [5,np.nan,np.nan,np.nan,np.nan,8,4],
'test2': [np.nan,8,3,np.nan,1,8,6], 'test3': [np.nan,np.nan,3,5,np.nan,8,np.nan]}
df = pd.DataFrame(array)
df.sort_values(by=['user', 'date'], ascending = True)
user date test1 test2 test3
1 John 2020-10-15 08:00:00 NaN 8.0 NaN
3 John 2020-10-19 08:00:00 NaN NaN 5.0
6 John 2020-11-16 09:12:00 4.0 6.0 NaN
4 Trevor 2020-10-10 08:00:00 NaN 1.0 NaN
0 Trevor 2020-10-11 08:00:00 5.0 NaN NaN
2 Trevor 2020-10-17 08:00:00 NaN 3.0 3.0
5 Trevor 2020-11-11 12:34:00 8.0 8.0 8.0
以下是所需的输出:
user test1 test2 test3
0 John 4 8 5
1 Trevor 5 1 3
我不完全理解“用同一用户来自同一列但在以后日期的数据填充空值”与您发布的所需输出之间的关系,但您可以通过
pivot\u table
获得所需信息:
# Added inplace=True
df.sort_values(by=['user', 'date'], ascending = True,inplace=True)
# Pivot table using 'first'
df.pivot_table(index='user',
aggfunc='first'). \
drop('date',axis=1)
如果我不理解您的意思,请纠正我。我不完全理解“用同一用户在同一列中的数据填充空值,但在以后的日期”与您发布的所需输出之间的关系,但是您可以通过
pivot\u table
获得所需信息:
# Added inplace=True
df.sort_values(by=['user', 'date'], ascending = True,inplace=True)
# Pivot table using 'first'
df.pivot_table(index='user',
aggfunc='first'). \
drop('date',axis=1)
如果我不理解你,请纠正我