[英]Replace missing value in a row if there's a match in two columns from another row using Pandas
I'm working on a data analysis project and I have the following dataframe that looks like this.我正在做一个数据分析项目,我有以下 dataframe 看起来像这样。
id ![]() |
store![]() |
long![]() |
lat![]() |
---|---|---|---|
1 ![]() |
A![]() |
1 ![]() |
-4 ![]() |
2 ![]() |
NaN![]() |
2 ![]() |
3 ![]() |
3 ![]() |
C ![]() |
4 ![]() |
5 ![]() |
4 ![]() |
D ![]() |
2 ![]() |
3 ![]() |
I want to fill the missing value NaN in the 'store' column with the one in row with id 4, given that row with id 2 and 4 have the same values in the 'long' and 'lat' columns, so the output should look like this我想用 id 为 4 的行中的一个填充“store”列中的缺失值 NaN,因为 id 为 2 和 4 的行在“long”和“lat”列中具有相同的值,因此 output 应该看起来像这样
id ![]() |
store![]() |
long![]() |
lat![]() |
---|---|---|---|
1 ![]() |
A![]() |
1 ![]() |
-4 ![]() |
2 ![]() |
D ![]() |
2 ![]() |
3 ![]() |
3 ![]() |
C ![]() |
4 ![]() |
5 ![]() |
4 ![]() |
D ![]() |
2 ![]() |
3 ![]() |
I want to do this for a long dataframe (almost a million rows), so I don't know the row ids that have the same 'long' and 'lat' values.我想为长 dataframe (几乎一百万行)执行此操作,所以我不知道具有相同“long”和“lat”值的行 ID。
I'm working on Python using Pandas.我正在使用 Pandas 研究 Python。 I've only come up with this solution using for loops and iterrows(), which is super slow
我只使用for 循环和 iterrows() 提出了这个解决方案,这非常慢
df_missing_names = df[df['store'].isna()] #rows that have missing names
df_with_names = df[df['store'].notna()] #rows that don't have missing names
for indx, row in df_missing_names.iterrows(): #run through all the rows that don't have names
for indx_j, row_j in df_with_names.iterrows(): #run through all the rows that have names
if (row.lat == row_j.lat) & (row.long == row_j.long): #if both lat and long values match
df[indx, 'store'] = row_j.store #then update name of the row in the original dataframe
Is there a faster way to do this using built in functions on Pandas?有没有更快的方法使用 Pandas 上的内置函数来执行此操作? Thanks for the help
谢谢您的帮助
You can use:您可以使用:
df['store'] = df.groupby(['long', 'lat'], sort=False).bfill()['store']
Output: Output:
id store long lat
0 1 A 1 -4
1 2 D 2 3
2 3 C 4 5
3 4 D 2 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.