需要在熊猫python中拆分的一列数据

问题描述 投票:0回答:1

我有一个看起来像这样的数据框:

0 movie*
1 star wars
2 the godfather
3 zombieland
4 book*
5 romeo and juliet
6 harry potter
7 tv series*
8 breaking bad
9 game of thrones
...

所以类别,后跟该类别中的项目,然后是其他类别,全部都在一个列中。假设这样的数据会在许多不同类别下持续一段时间。

然后我想要将数据构造为:

  type        name

0 movie*      star wars   
1 movie*      godfather
2 movie*      zombieland
3 book*       romeo and juliet
4 book*       harry potter
5 tv series*  breaking bad
6 tv series*  game of thrones
...

我已经用.endswith('*')实现了布尔型掩码,但是不知道如何将其合并到新的数据框中。

python pandas
1个回答
0
投票

用途:

print (df)
               name
0            movie*
1         star wars
2     the godfather
3        zombieland
4             book*
5  romeo and juliet
6      harry potter
7        tv series*
8      breaking bad
9   game of thrones

df.insert(0, 'type', df['name'].where(df['name'].str.endswith('*')).ffill())
df = df[df['type'].ne(df['name'])].copy()
df['type'] = df['type'].str.strip('*')
print (df)
        type              name
1      movie         star wars
2      movie     the godfather
3      movie        zombieland
5       book  romeo and juliet
6       book      harry potter
8  tv series      breaking bad
9  tv series   game of thrones
© www.soinside.com 2019 - 2024. All rights reserved.