简体   繁体   English

如何通过搜索现有列值而不迭代在数据框中追加新行?

[英]How to append a new row in a dataframe by searching for an existing column value without iterating?

I'm trying to find the best way to create new rows for every 1 row when a certain value is contained in a column.当某个值包含在列中时,我试图找到为每 1 行创建新行的最佳方法。

Example Dataframe示例数据框

Index指数 Person Drink_Order Drink_Order
1 1 Sam山姆 Jack and Coke杰克和可乐
2 2 John约翰 Coke可乐
3 3 Steve史蒂夫 Dr. Pepper胡椒博士

I'd like to search the DataFrame for Jack and Coke, remove it and add 2 new records as Jack and Coke are 2 different drink sources.我想在 DataFrame 中搜索 Jack 和 Coke,将其删除并添加 2 个新记录,因为 Jack 和 Coke 是 2 个不同的饮料来源。

Index指数 Person Drink_Order Drink_Order
2 2 John约翰 Coke可乐
3 3 Steve史蒂夫 Dr. Pepper胡椒博士
4 4 Sam山姆 Jack Daniels杰克丹尼
5 5 Sam山姆 Coke可乐

Example Code that I want to replace as my understanding is you should never modify rows you are iterating我想替换的示例代码,因为我的理解是你永远不应该修改你正在迭代的行

for index, row in df.loc[df['Drink_Order'].str.contains('Jack and Coke')].iterrows():
    df.loc[len(df)]=[row['Person'],'Jack Daniels']
    df.loc[len(df)]=[row['Person'],'Coke']

df = df[df['Drink_Order']!= 'Jack and Coke']

Split using and.使用和拆分。 That will result in a list.这将导致一个列表。 Explode list to get each element in a list appear as an individual row.分解列表以获取列表中的每个元素显示为单独的行。 Then conditionally rename Jack to Jack Daniels然后有条件地将 Jack 重命名为 Jack Daniels

 df= df.assign(Drink_Order=df['Drink_Order'].str.split('and')).explode('Drink_Order')
df['Drink_Order']=np.where(df['Drink_Order'].str.contains('Jack'),'Jack Daniels',df['Drink_Order'])

    Index Person   Drink_Order
0      1    Sam  Jack Daniels
0      1    Sam          Coke
1      2   John          Coke
2      3  Steve    Dr. Pepper

暂无
暂无

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

相关问题 如何通过遍历 Python 中的 dataframe 中的每一行来将计算值存储在新列中? - How to store a calculated value in new column by iterating through each row in a dataframe in Python? 如果列值为“ foo”,则在同一行上将数据框追加新值吗? - If column value is “foo”, append dataframe with new values on the same row? 在 Pandas dataframe 中,如何根据每一行的值在 append 中创建一个新的 True / False 列? - In Pandas dataframe, how to append a new column of True / False based on each row's value? append dataframe 将新数据添加到现有列 - append dataframe to add new data to existing column 如何将遍历列表的新结果附加到数据框中的新列中 - How may I append new results from iterating through a list, into a new column in the dataframe 如何在单列上附加pandas数据帧的新行? - How to append a new row on pandas dataframe with single column? 有没有办法向pandas数据框添加新列,将新列的每个唯一值附加到数据帧的每个现有行? - Is there a way to add a new column to a pandas dataframe, appending each unique value of the new column to every existing row of the dataframe? Append 条件后 dataframe 中现有列的值 - Append a value to an existing column in a dataframe after conditions 将新值追加到MySQL中列的现有值 - Append new value to existing value of column in MySQL 如何在不覆盖的情况下将新值附加到现有的dict键 - how to append a new value to the existing dict key without overwrite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM