pandas 数据框删除每个数字的第一行 [英] pandas data frame removing the first row of every numbers

查看:87
本文介绍了 pandas 数据框删除每个数字的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我有一个数据框,其第一列如下所示:

So, basically I have a data frame that has the first column looks like this:

#1
#2 
#2
#3
#3
#3
#3
#4
#4
#5

如您所见,第一列由随机重复的数字组成.

As you can see, first column is consisting of randomly repeated numbers.

首先,我必须删除所有的#",然后必须删除每个数字的第一行.因为#1只有一行,所以它应该消失,而#2的第二行仍然保留,依此类推.

First, I have to remove all the '#' then I have to remove the first row of each numbers. Since, #1 has only one row, it should go away, and only the second row of #2 remains,,, so on.

推荐答案

使用 duplicated boolean indexing ,最后按str[1:]的位置或通过#. strip.html"rel =" nofollow noreferrer> str.strip :

Use duplicated with boolean indexing, last remove # by position with str[1:] or by str.strip:

print (df)
    a
0  #1
1  #2
2  #2
3  #3
4  #3
5  #3
6  #3
7  #4
8  #4
9  #5


df = df.loc[df['a'].duplicated(), 'a'].str[1:]
print (df)
2    2
4    3
5    3
6    3
8    4
Name: a, dtype: object

或者:

df = df.loc[df['a'].duplicated(), 'a'].str.strip('#')
print (df)
2    2
4    3
5    3
6    3
8    4
Name: a, dtype: object

详细信息:

print (df['a'].duplicated())
0    False
1    False
2     True
3    False
4     True
5     True
6     True
7    False
8     True
9    False
Name: a, dtype: bool

df = df[df['a'].duplicated()]
df['a'] = df['a'].str.strip('#')

这篇关于 pandas 数据框删除每个数字的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆