大熊猫:按组插入空白行和具有索引的行?

时间:2020-01-07 06:46:40

标签: python pandas dataframe

我试图弄清楚如何在每个组之后插入空白行和具有索引的行。 我能够插入空白行(由于SO上的引用),但是,在弄清楚如何插入带有索引的另一行时遇到了麻烦。

原始


+------+---------+-------+--+
|  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)

非常感谢 问候

2 个答案:

答案 0 :(得分:4)

添加自定义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

答案 1 :(得分:1)

不是最有效的方法:

http://sitename/_api/web/lists/getbytitle('csutom list')/views?$filter=PersonalView eq true

输出:

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])
相关问题