如何使用左列的数据作为参考填充NA

发布于 2025-02-13 05:50:53 字数 712 浏览 0 评论 0原文

id喜欢寻求帮助以修复熊猫数据框中(python)中的丢失值

这是数据集

在此数据集中,我在['item_weight']列中找到了一个缺少的值。

我不想放下缺失的值,因为我通过对它们进行排序。缺少的值是编码它的人的“小姐类型”。

这是排序的数据集

现在我创建了一个查找数据集,以便可以合并它们以填充NA缺失值。

我如何使用我制作的查找表合并或加入它们以填充缺失值(NAN)?还是没有使用查找表的其他方法?

Id like to ask for help in fixing the missing values in pandas dataframe (python)

here is the dataset
dataset

In this dataset I found a missing value in ['Item_Weight'] column.

I don't want to drop the missing values because I found out by sorting them. the missing value is "miss type" by someone who encoded it.

here is the sorted dataset
sorted_dataset

Now I created a lookup dataset so I can merge them to fill na missing values.

Lookup Table

How can I merge them or join them only to fill the missing values (Nan) using the lookup table I made? Or is there any other way without using a lookup table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰魂雪魄 2025-02-20 05:50:53

从中看,您可能希望使用MAP而不是JOIN/MERGE使用的内容,这是如何将映射与数据一起使用的示例。

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'Column1' : ['A', 'B', 'C'],
    'Column2' : [1, np.nan, 3]
})
df

df_map = pd.DataFrame({
    'Column1' : ['A', 'B', 'C'],
    'Column2' : [1, 2, 3]
})

df_map

#Looks to find where the column you specify is null, then using your map df will map the value from column1 to column2
df['Column2'] = np.where(df['Column2'].isna(), df['Column1'].map(df_map.set_index('Column1')['Column2']), df['Column2'])

由于您使用了屏幕截图,因此我必须创建自己的数据框。将来,屏幕截图的使用被认为是帮助开发人员提供帮助的最佳选择。

Looking at this you will probably want to use something along the lines of map instead of join/merge this is an example of how to use map with your data.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'Column1' : ['A', 'B', 'C'],
    'Column2' : [1, np.nan, 3]
})
df

df_map = pd.DataFrame({
    'Column1' : ['A', 'B', 'C'],
    'Column2' : [1, 2, 3]
})

df_map

#Looks to find where the column you specify is null, then using your map df will map the value from column1 to column2
df['Column2'] = np.where(df['Column2'].isna(), df['Column1'].map(df_map.set_index('Column1')['Column2']), df['Column2'])

I had to create my own dataframes since you used screenshots. In the future, the use of screenshots is not considered best to help developers with assistance.

静待花开 2025-02-20 05:50:53

这可能会起作用:

df = df.sort_values(['Item_Identifier', 'Item_Weight']).ffill()

但是我无法对其进行测试,因为您没有给我们任何工作。

This will probably work:

df = df.sort_values(['Item_Identifier', 'Item_Weight']).ffill()

But I can't test it since you didn't give us anything to work with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文