How to remove rows in a Pandas Dataframe with a specific column containing numbers only?
up vote
3
down vote
favorite
Lets say I have this DF:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
2 123456789 08/03/2011
3 Nameless101 07/12/2012
I want to be able to remove all the rows in the DF where the IGName
is only numbers.
Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.
I want the result to look like this:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
3 Nameless101 07/12/2012
python pandas
add a comment |
up vote
3
down vote
favorite
Lets say I have this DF:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
2 123456789 08/03/2011
3 Nameless101 07/12/2012
I want to be able to remove all the rows in the DF where the IGName
is only numbers.
Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.
I want the result to look like this:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
3 Nameless101 07/12/2012
python pandas
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Lets say I have this DF:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
2 123456789 08/03/2011
3 Nameless101 07/12/2012
I want to be able to remove all the rows in the DF where the IGName
is only numbers.
Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.
I want the result to look like this:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
3 Nameless101 07/12/2012
python pandas
Lets say I have this DF:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
2 123456789 08/03/2011
3 Nameless101 07/12/2012
I want to be able to remove all the rows in the DF where the IGName
is only numbers.
Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.
I want the result to look like this:
ID IGName Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
3 Nameless101 07/12/2012
python pandas
python pandas
asked Nov 20 at 1:16
The Dodo
738
738
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You could do:
import pandas as pd
data = [[0, 'BananaMan', '09/10/2018'],
[1, 'Superman247', '10/10/2009'],
[2, '123456789', '08/03/2011'],
[3, 'Nameless101', '07/12/2012']]
df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])
df = df[~df['IGName'].str.isnumeric()]
print(df)
Output
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
From the documentation:
Check whether all characters in each string in the Series/Index are
numeric. Equivalent to str.isnumeric().
Note that this solution assumes the column 'IGName'
is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):
df['IGName'] = df['IGName'].astype(str)
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
I mean if you have mixed types in the same column. For example, if in your data, you had[2, 123456789, '08/03/2011']
instead of[2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 at 1:35
|
show 1 more comment
up vote
0
down vote
Use df[...]
:
print(df[~df['IGName'].str.isnumeric()])
Or:
print(df[df['IGName'].str.contains(r'D+')])
Both Output:
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
If IGName
has integers do:
print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])
1
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You could do:
import pandas as pd
data = [[0, 'BananaMan', '09/10/2018'],
[1, 'Superman247', '10/10/2009'],
[2, '123456789', '08/03/2011'],
[3, 'Nameless101', '07/12/2012']]
df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])
df = df[~df['IGName'].str.isnumeric()]
print(df)
Output
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
From the documentation:
Check whether all characters in each string in the Series/Index are
numeric. Equivalent to str.isnumeric().
Note that this solution assumes the column 'IGName'
is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):
df['IGName'] = df['IGName'].astype(str)
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
I mean if you have mixed types in the same column. For example, if in your data, you had[2, 123456789, '08/03/2011']
instead of[2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 at 1:35
|
show 1 more comment
up vote
4
down vote
accepted
You could do:
import pandas as pd
data = [[0, 'BananaMan', '09/10/2018'],
[1, 'Superman247', '10/10/2009'],
[2, '123456789', '08/03/2011'],
[3, 'Nameless101', '07/12/2012']]
df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])
df = df[~df['IGName'].str.isnumeric()]
print(df)
Output
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
From the documentation:
Check whether all characters in each string in the Series/Index are
numeric. Equivalent to str.isnumeric().
Note that this solution assumes the column 'IGName'
is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):
df['IGName'] = df['IGName'].astype(str)
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
I mean if you have mixed types in the same column. For example, if in your data, you had[2, 123456789, '08/03/2011']
instead of[2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 at 1:35
|
show 1 more comment
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You could do:
import pandas as pd
data = [[0, 'BananaMan', '09/10/2018'],
[1, 'Superman247', '10/10/2009'],
[2, '123456789', '08/03/2011'],
[3, 'Nameless101', '07/12/2012']]
df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])
df = df[~df['IGName'].str.isnumeric()]
print(df)
Output
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
From the documentation:
Check whether all characters in each string in the Series/Index are
numeric. Equivalent to str.isnumeric().
Note that this solution assumes the column 'IGName'
is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):
df['IGName'] = df['IGName'].astype(str)
You could do:
import pandas as pd
data = [[0, 'BananaMan', '09/10/2018'],
[1, 'Superman247', '10/10/2009'],
[2, '123456789', '08/03/2011'],
[3, 'Nameless101', '07/12/2012']]
df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])
df = df[~df['IGName'].str.isnumeric()]
print(df)
Output
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
From the documentation:
Check whether all characters in each string in the Series/Index are
numeric. Equivalent to str.isnumeric().
Note that this solution assumes the column 'IGName'
is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):
df['IGName'] = df['IGName'].astype(str)
edited Nov 20 at 1:38
answered Nov 20 at 1:25
Daniel Mesejo
9,9631923
9,9631923
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
I mean if you have mixed types in the same column. For example, if in your data, you had[2, 123456789, '08/03/2011']
instead of[2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 at 1:35
|
show 1 more comment
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
I mean if you have mixed types in the same column. For example, if in your data, you had[2, 123456789, '08/03/2011']
instead of[2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 at 1:35
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 at 1:31
I mean if you have mixed types in the same column. For example, if in your data, you had
[2, 123456789, '08/03/2011']
instead of [2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
I mean if you have mixed types in the same column. For example, if in your data, you had
[2, 123456789, '08/03/2011']
instead of [2, '123456789', '08/03/2011']
– RafaelC
Nov 20 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 at 1:34
As a matter of fact, yes ;}. Use
.astype(str)
– RafaelC
Nov 20 at 1:35
As a matter of fact, yes ;}. Use
.astype(str)
– RafaelC
Nov 20 at 1:35
|
show 1 more comment
up vote
0
down vote
Use df[...]
:
print(df[~df['IGName'].str.isnumeric()])
Or:
print(df[df['IGName'].str.contains(r'D+')])
Both Output:
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
If IGName
has integers do:
print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])
1
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
add a comment |
up vote
0
down vote
Use df[...]
:
print(df[~df['IGName'].str.isnumeric()])
Or:
print(df[df['IGName'].str.contains(r'D+')])
Both Output:
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
If IGName
has integers do:
print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])
1
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
add a comment |
up vote
0
down vote
up vote
0
down vote
Use df[...]
:
print(df[~df['IGName'].str.isnumeric()])
Or:
print(df[df['IGName'].str.contains(r'D+')])
Both Output:
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
If IGName
has integers do:
print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])
Use df[...]
:
print(df[~df['IGName'].str.isnumeric()])
Or:
print(df[df['IGName'].str.contains(r'D+')])
Both Output:
ID IGName Date_created
0 0 BananaMan 09/10/2018
1 1 Superman247 10/10/2009
3 3 Nameless101 07/12/2012
If IGName
has integers do:
print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])
edited Nov 20 at 1:45
answered Nov 20 at 1:22
U9-Forward
10.5k2834
10.5k2834
1
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
add a comment |
1
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
1
1
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
Same observation from above applies here
– RafaelC
Nov 20 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
@RafaelC Now is it good.
– U9-Forward
Nov 20 at 1:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384890%2fhow-to-remove-rows-in-a-pandas-dataframe-with-a-specific-column-containing-numbe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown