简体   繁体   English

如何通过匹配来自另一个数据帧熊猫的值来填充数据帧中的列的值

[英]How to fill in values for a column in a dataframe by matching values from another dataframe pandas

I'm new to python and am working with the kaggle titanic dataset to practice.我是 Python 新手,正在使用 kaggle 泰坦尼克号数据集进行练习。

I'm trying to fill in a couple missing values for the cabin feature by using rows that have the same tickets.我正在尝试通过使用具有相同票证的行来填充客舱功能的几个缺失值。 That is, I want to get a list of duplicate tickets and their corresponding cabin value and replace the null values with the cabin values corresponding to the same ticket.也就是说,我想获取重复机票及其相应舱位值的列表,并将空值替换为与同一张票对应的舱位值。

In my approach, I created a dataframe with the following code consisting of only one occurrence of the duplicate ticket(given that the ticket had a cabin value to go along with it; is non-null) to assign it a single cabin value.在我的方法中,我使用以下代码创建了一个数据框,其中仅包含一次重复机票(假设机票有一个客舱值与之配套;非空),以为其分配一个客舱值。 This way I could fill in the cabin values in the training set(maindf) by matching.这样我就可以通过匹配来填充训练集(maindf)中的客舱值。

ticket_dupl = maindf[(maindf.duplicated('Ticket')) & (maindf['Cabin'].notnull())][['Ticket','Cabin']].drop_duplicates('Ticket')

This gives me a dataframe of length 50 with index perserved, heres the first 7 rows:这给了我一个长度为 50 的数据帧,并保留了索引,这是前 7 行:

    Ticket  Cabin
88  19950   C23 C25 C27
124 35281   D26
137 113803  C123
193 230080  F2
195 PC 17569 B80
230 36973   C83
251 347054  G6

Is there a way to fill in some cabin values in my maindf by matching ticket rows or indices, preserving the values for which tickets don't match?有没有办法通过匹配票行或索引来填充我的 maindf 中的一些客舱值,保留不匹配的票的值? Can't seem to understand from other solutions for questions similar to mine.对于与我类似的问题,似乎无法从其他解决方案中理解。

Also, I was wondering if there was a more efficient way of achieving my goal instead of creating a dataframe like I did.另外,我想知道是否有更有效的方法来实现我的目标,而不是像我那样创建数据框。 Thanks.谢谢。

您可以按故障单分组以将具有匹配故障单的行组合在一起,并使用返回组中第一个非空值的 first_valid_index 填充空值。

df['Cabin'] = df.groupby('Ticket')['Cabin'].transform(lambda x: x.loc[x.first_valid_index()])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果其他两个列在Pandas中具有匹配的值,如何用另一个数据框的值填充空列的值? - How to fill empty column values with another dataframe's value if two other columns have matching values in Pandas? 如何使用不同的 datetimeindex 将 pandas dataframe 中的值填充到另一个 dataframe - How to fill values from a pandas dataframe to another dataframe with different datetimeindex 基于匹配来自另一个数据帧pandas的值的新列 - New column based on matching values from another dataframe pandas 将Pandas DataFrame列值与另一个DataFrame列匹配 - Matching Pandas DataFrame Column Values with another DataFrame Column 如何在 pandas dataframe 中填充 null 值,并使用 Z23EEEB4347BDD752BFC6B7EEDZ 中另一列的值? - How to fill null values in pandas dataframe, with values from another column in python? 通过匹配另一个DataFrame中的值来最佳填充Pandas DataFrame列 - Optimal filling of pandas DataFrame column by matching values in another DataFrame 如何根据另外两个数据帧的值填充 Pandas 数据帧 - How to fill the Pandas Dataframe based on values from another two dataframes 熊猫用另一个数据框范围内的值计数填充数据框 - Pandas fill dataframe with count of values within a range from another dataframe Pandas 从另一个 dataframe 填充 dataframe 中的缺失值 - Pandas fill missing values in dataframe from another dataframe 熊猫从另一个数据帧填充一个数据帧上的空值 - Pandas fill empty values on one dataframe from another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM