How to convert Decimal128 to decimal in pandas dataframe
up vote
0
down vote
favorite
I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').
What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?
There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?
Edit, I've tried:
testdf = my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)
testdf[testdf["MyCol"] > 80].head()
but I get:
TypeError: '>' not supported between instances of 'Decimal128' and 'int'
Converting a single column using .astype(str).astype(float) works.
python pandas numpy
add a comment |
up vote
0
down vote
favorite
I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').
What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?
There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?
Edit, I've tried:
testdf = my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)
testdf[testdf["MyCol"] > 80].head()
but I get:
TypeError: '>' not supported between instances of 'Decimal128' and 'int'
Converting a single column using .astype(str).astype(float) works.
python pandas numpy
I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago
they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago
Can you show how your dataframe looks?
– Mohit Motwani
2 days ago
looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago
1
Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').
What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?
There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?
Edit, I've tried:
testdf = my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)
testdf[testdf["MyCol"] > 80].head()
but I get:
TypeError: '>' not supported between instances of 'Decimal128' and 'int'
Converting a single column using .astype(str).astype(float) works.
python pandas numpy
I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').
What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?
There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?
Edit, I've tried:
testdf = my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)
testdf[testdf["MyCol"] > 80].head()
but I get:
TypeError: '>' not supported between instances of 'Decimal128' and 'int'
Converting a single column using .astype(str).astype(float) works.
python pandas numpy
python pandas numpy
edited 2 days ago
asked Nov 17 at 13:30
Rich
2,29911218
2,29911218
I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago
they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago
Can you show how your dataframe looks?
– Mohit Motwani
2 days ago
looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago
1
Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago
add a comment |
I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago
they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago
Can you show how your dataframe looks?
– Mohit Motwani
2 days ago
looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago
1
Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago
I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago
I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago
they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago
they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago
Can you show how your dataframe looks?
– Mohit Motwani
2 days ago
Can you show how your dataframe looks?
– Mohit Motwani
2 days ago
looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago
looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago
1
1
Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago
Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Casting full DataFrame.
df = df.astype(str).astype(float)
For single column. IDs is the name of the column.
df["IDs"] = df.IDs.astype(str).astype(float)
Test implementation
from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)
Output:
[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]
D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
add a comment |
up vote
1
down vote
Just use:
df = df.astype(float)
You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.
df = df.applymap(float)
I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?
df = df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)
It will check if a column is of type Decimal128 and then convert it to float.
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Thanks, but no luck, I've updated my question
– Rich
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Casting full DataFrame.
df = df.astype(str).astype(float)
For single column. IDs is the name of the column.
df["IDs"] = df.IDs.astype(str).astype(float)
Test implementation
from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)
Output:
[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]
D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
add a comment |
up vote
1
down vote
accepted
Casting full DataFrame.
df = df.astype(str).astype(float)
For single column. IDs is the name of the column.
df["IDs"] = df.IDs.astype(str).astype(float)
Test implementation
from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)
Output:
[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]
D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Casting full DataFrame.
df = df.astype(str).astype(float)
For single column. IDs is the name of the column.
df["IDs"] = df.IDs.astype(str).astype(float)
Test implementation
from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)
Output:
[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]
D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143
Casting full DataFrame.
df = df.astype(str).astype(float)
For single column. IDs is the name of the column.
df["IDs"] = df.IDs.astype(str).astype(float)
Test implementation
from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)
Output:
[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]
D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143
edited Nov 17 at 16:37
answered Nov 17 at 13:38
Chirag
571210
571210
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
add a comment |
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06
add a comment |
up vote
1
down vote
Just use:
df = df.astype(float)
You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.
df = df.applymap(float)
I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?
df = df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)
It will check if a column is of type Decimal128 and then convert it to float.
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Thanks, but no luck, I've updated my question
– Rich
2 days ago
add a comment |
up vote
1
down vote
Just use:
df = df.astype(float)
You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.
df = df.applymap(float)
I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?
df = df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)
It will check if a column is of type Decimal128 and then convert it to float.
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Thanks, but no luck, I've updated my question
– Rich
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
Just use:
df = df.astype(float)
You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.
df = df.applymap(float)
I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?
df = df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)
It will check if a column is of type Decimal128 and then convert it to float.
Just use:
df = df.astype(float)
You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.
df = df.applymap(float)
I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?
df = df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)
It will check if a column is of type Decimal128 and then convert it to float.
edited Nov 17 at 19:44
answered Nov 17 at 13:38
Mohit Motwani
612319
612319
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Thanks, but no luck, I've updated my question
– Rich
2 days ago
add a comment |
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Thanks, but no luck, I've updated my question
– Rich
2 days ago
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45
Thanks, but no luck, I've updated my question
– Rich
2 days ago
Thanks, but no luck, I've updated my question
– Rich
2 days ago
add a comment |
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%2f53351707%2fhow-to-convert-decimal128-to-decimal-in-pandas-dataframe%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
I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago
they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago
Can you show how your dataframe looks?
– Mohit Motwani
2 days ago
looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago
1
Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago