How to remove rows in a Pandas Dataframe with a specific column containing numbers only?
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 |
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 |
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 '18 at 1:16
The DodoThe Dodo
1399
1399
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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 '18 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 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 '18 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 '18 at 1:35
|
show 1 more comment
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 '18 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 at 1:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 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 '18 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 '18 at 1:35
|
show 1 more comment
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 '18 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 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 '18 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 '18 at 1:35
|
show 1 more comment
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 '18 at 1:38
answered Nov 20 '18 at 1:25
Daniel MesejoDaniel Mesejo
17.2k21431
17.2k21431
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 '18 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 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 '18 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 '18 at 1:35
|
show 1 more comment
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 '18 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 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 '18 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
As a matter of fact, yes ;}. Use.astype(str)
– RafaelC
Nov 20 '18 at 1:35
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 '18 at 1:29
This will fail if numbers are actually numbers (int or float type).
– RafaelC
Nov 20 '18 at 1:29
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 at 1:31
@RafaelC do you mean if the column is of type int or float?
– Daniel Mesejo
Nov 20 '18 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 '18 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 '18 at 1:33
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
Does it suffices to cast the column to string first?
– Daniel Mesejo
Nov 20 '18 at 1:34
As a matter of fact, yes ;}. Use
.astype(str)
– RafaelC
Nov 20 '18 at 1:35
As a matter of fact, yes ;}. Use
.astype(str)
– RafaelC
Nov 20 '18 at 1:35
|
show 1 more comment
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 '18 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 at 1:45
add a comment |
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 '18 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 at 1:45
add a comment |
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 '18 at 1:45
answered Nov 20 '18 at 1:22
U9-ForwardU9-Forward
14.8k41338
14.8k41338
1
Same observation from above applies here
– RafaelC
Nov 20 '18 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 at 1:45
add a comment |
1
Same observation from above applies here
– RafaelC
Nov 20 '18 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 at 1:45
1
1
Same observation from above applies here
– RafaelC
Nov 20 '18 at 1:36
Same observation from above applies here
– RafaelC
Nov 20 '18 at 1:36
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 at 1:45
@RafaelC Now is it good.
– U9-Forward
Nov 20 '18 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.
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