[英]How to add a row at the top of each category by concatenating the new data?
我有 2 個不同大小的數據集(CSV 格式),如下所示:
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:
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
我想將df_new
連接到df_old
,但是df_new
位於df_old
的每個類別之上。 預期的 output 應該是:
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
我試過了,但這不是我想要的,因為它將所有內容都添加到頂部,而不是每個類別。
你可以試試:
(pd.concat([df_new,df_old], sort=False)
.sort_values('category', ascending=False, kind='mergesort')
)
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.