简体   繁体   English

如何通过连接新数据在每个类别的顶部添加一行?

[英]How to add a row at the top of each category by concatenating the new data?

I have 2 datasets (in CSV format) with different size such as follow:我有 2 个不同大小的数据集(CSV 格式),如下所示:

df_old: df_old:

index category  text
 0    spam      you win much money
 1    spam      you win 7000 car
 2    spam      you are the winner of the game
 3    not_spam  the weather in Chicago is nice
 4    not_spam  Paris is the capital of France
 5    not_spam  pizza is an Italian food
 6    neutral   we have a party now
 7    neutral   they are driving to downtown
 

df_new: df_new:

index category  text
 12   spam      we are the cheapest insurance
 14   spam      London is the capital of Canada
 15   not_spam  no more raining in winter
 25   not_spam  the soccer game plays on HBO
 31   neutral   construction will be done
 

I would like to concatenate df_new to the df_old but in the way that df_new goes on top of df_old 's each category.我想将df_new连接到df_old ,但是df_new位于df_old的每个类别之上。 The expected output should be:预期的 output 应该是:

df_concat: df_concat:

index category  text
 12   spam      we are the cheapest insurance
 14   spam      London is the capital of Canada
 0    spam      you win much money
 1    spam      you win 7000 car
 2    spam      you are the winner of the game
 15   not_spam  no more raining in winter
 25   not_spam  the soccer game plays on HBO
 3    not_spam  the weather in Chicago is nice
 4    not_spam  Paris is the capital of France
 5    not_spam  pizza is an Italian food
 31   neutral   construction will be done
 6    neutral   we have a party now
 7    neutral   they are driving to downtown

I tried this , and it not what i want as it is adding everything to the top, not on each category.我试过,但这不是我想要的,因为它将所有内容都添加到顶部,而不是每个类别。

You can try:你可以试试:

(pd.concat([df_new,df_old], sort=False)
   .sort_values('category', ascending=False, kind='mergesort')
)

Output: Output:

   index  category                             text
0     12      spam    we are the cheapest insurance
1     14      spam  London is the capital of Canada
0      0      spam               you win much money
1      1      spam                 you win 7000 car
2      2      spam   you are the winner of the game
2     15  not_spam        no more raining in winter
3     25  not_spam     the soccer game plays on HBO
3      3  not_spam   the weather in Chicago is nice
4      4  not_spam   Paris is the capital of France
5      5  not_spam         pizza is an Italian food
4     31   neutral        construction will be done
6      6   neutral              we have a party now
7      7   neutral     they are driving to downtown

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

相关问题 在每个数据对集的顶部添加一个标题行 - add a title row on top of each data pairs sets 将字符串与数据框中的每一行连接起来并对其应用函数? - Concatenating string with each row in data frame and applying function to them? 如何将熊猫数据框添加到每一行? - How to add pandas data frame to each row? 如何为熊猫中的每个不同 ID 添加新行? - how to add add new row for each distinct ID in pandas? 如何将新列添加到数据框并将数据逐行添加到其中 - how to add new column to a dataframe and add the data to it row by row 如何在彼此之上添加数据框? - How do i add data frames on top of each other? 如何在新行中制作每个数据 [data to csv]? - How can I make each data in new row [data to csv]? 如何在PANDAS中的每组groupby中添加新行,该行的值之一是每组值的总和 - how to add new row into each group of groupby in PANDAS , one of the value of that row is sum of values of each groups 如何根据每行中的数据以及满足特定条件的其他行的存在向 Pandas Dataframe 添加新列? - How to add a new column to a Pandas Dataframe based on data both in each row, and on the existence of other rows that meet a specific criteria? 如何在 python df 中的特定列的每一行中创建一个具有前 3 个最大值的新列? - How to create a new columns with the top 3 maximum values in each row from specific columns in python df?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM