Pandas:插入一个空白行和一个按组索引的行?

2024-05-23 22:29:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何在每组后面插入一个空行和一个带索引的行。 我能够插入一个空行(感谢上面的引用),但是,我很难想出如何插入另一个带有索引的行

原件:


+------+---------+-------+--+
|  id  | country | score |  |
+------+---------+-------+--+
| 1011 | JPN     |     5 |  |
| 1011 | JPN     |     5 |  |
| 1011 | NZ      |     4 |  |
| 1011 | NZ      |     5 |  |
| 1012 | NZ      |     5 |  |
| 1012 | AUS     |     6 |  |
| 1012 | NZ      |     6 |  |
| 1013 | AUS     |     5 |  |
| 1013 | AUS     |     5 |  |
+------+---------+-------+--+

插入一个空行


+------+---------+-------+
|  id  | country | score |
+------+---------+-------+
| 1011 | JPN     |     5 |
| 1011 | JPN     |     5 |
| 1011 | NZ      |     4 |
| 1011 | NZ      |     5 |
|      |         |       |
| 1012 | NZ      |     5 |
| 1012 | AUS     |     6 |
| 1012 | NZ      |     6 |
|      |         |       |
| 1013 | AUS     |     5 |
| 1013 | AUS     |     5 |
+------+---------+-------+

所需输出:


+------+---------+-------+
|  id  | country | score |
+------+---------+-------+
| 1011 | JPN     | 5     |
| 1011 | JPN     | 5     |
| 1011 | NZ      | 4     |
| 1011 | NZ      | 5     |
|      |         |       |
| id   | country | score |
| 1012 | NZ      | 5     |
| 1012 | AUS     | 6     |
| 1012 | NZ      | 6     |
|      |         |       |
| id   | country | score |
| 1013 | AUS     | 5     |
| 1013 | AUS     | 5     |
+------+---------+-------+

原始DF:

import pandas as pd
import numpy as np
data = {'id':[1011,1011,1011,1011,1012,1012,1012,1013,1013],
'country':[JPN,JPN,NZ,NZ,NZ,AUS,NZ,AUS,AUS]
,'score':[5,5,4,5,5,6,6,5,5]}
df = pd.DataFrame(data)

插入空行的DF:

df1= df.groupby('id').apply(lambda d: d.append({'id': d.name}, ignore_index=True).astype({'id': int})).reset_index(drop=True)

非常感谢 问候


Tags: importidtruedfdataindexascountry
2条回答

通过添加自定义DataFrame,可以通过iloc最后删除最后2行:

df2 = pd.DataFrame([[''] * len(df.columns), df.columns], columns=df.columns)
df1= (df.groupby('id', group_keys=False)
        .apply(lambda d: d.append(df2))
        .iloc[:-2]
        .reset_index(drop=True))
print (df1)
      id  country  score
0   1011      JPN      5
1   1011      JPN      5
2   1011       NZ      4
3   1011       NZ      5
4                       
5     id  country  score
6   1012       NZ      5
7   1012      AUS      6
8   1012       NZ      6
9                       
10    id  country  score
11  1013      AUS      5
12  1013      AUS      5

不是最有效的方法:

print(df.groupby('id', as_index=False).apply(lambda x: x.append(dict.fromkeys(data.keys(), ''), ignore_index=True).append({k:k for k in data.keys()}, ignore_index=True)).reset_index(drop=True).iloc[:-1])

输出:

    country    id  score
0       JPN  1011      5
1       JPN  1011      5
2        NZ  1011      4
3        NZ  1011      5
4                       
5   country    id  score
6        NZ  1012      5
7       AUS  1012      6
8        NZ  1012      6
9                       
10  country    id  score
11      AUS  1013      5
12      AUS  1013      5
13               

相关问题 更多 >