How to use math.log10() function on whole pandas dataframe
I want to do logarithm of every value in pandas dataframe. I have tried this but it does not work:
#Reading data from excel and rounding values on 2 decimal places
import math
import pandas as pd
data = pd.read_excel("DataSet.xls").round(2)
log_data= math.log10(data)
It gives me this error:
TypeError: must be real number, not DataFrame
Do you have any idea what to do?
python pandas
add a comment |
I want to do logarithm of every value in pandas dataframe. I have tried this but it does not work:
#Reading data from excel and rounding values on 2 decimal places
import math
import pandas as pd
data = pd.read_excel("DataSet.xls").round(2)
log_data= math.log10(data)
It gives me this error:
TypeError: must be real number, not DataFrame
Do you have any idea what to do?
python pandas
add a comment |
I want to do logarithm of every value in pandas dataframe. I have tried this but it does not work:
#Reading data from excel and rounding values on 2 decimal places
import math
import pandas as pd
data = pd.read_excel("DataSet.xls").round(2)
log_data= math.log10(data)
It gives me this error:
TypeError: must be real number, not DataFrame
Do you have any idea what to do?
python pandas
I want to do logarithm of every value in pandas dataframe. I have tried this but it does not work:
#Reading data from excel and rounding values on 2 decimal places
import math
import pandas as pd
data = pd.read_excel("DataSet.xls").round(2)
log_data= math.log10(data)
It gives me this error:
TypeError: must be real number, not DataFrame
Do you have any idea what to do?
python pandas
python pandas
asked 1 hour ago
AleksandarAleksandar
908
908
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Use the numpy version, not math
import numpy as np
np.log10(df)
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
From what it seems math.log10
cannot handle neither dataframes nor ndarrays
.
So you can either go with np.log10
and reconstruct the dataframe as pointed out in other solutions, or if you want to go with math.log10
, you can use DataFrame.applymap
to apply math.log10
to the dataframe elementwise. Note however that this solution will be slower than a vectorized approach using np.log10
.
df.applymap(math.log10)
Example
df = pd.DataFrame(np.random.randint(1,5,(6,6)), columns=list('abcdef'))
print(df)
a b c d e f
0 3 4 1 1 2 1
1 4 4 4 3 4 1
2 4 3 3 1 4 1
3 3 4 1 3 1 1
4 1 2 3 4 2 1
5 1 3 3 1 4 3
df.applymap(math.log10)
a b c d e f
0 0.477121 0.602060 0.000000 0.000000 0.30103 0.000000
1 0.602060 0.602060 0.602060 0.477121 0.60206 0.000000
2 0.602060 0.477121 0.477121 0.000000 0.60206 0.000000
3 0.477121 0.602060 0.000000 0.477121 0.00000 0.000000
4 0.000000 0.301030 0.477121 0.602060 0.30103 0.000000
5 0.000000 0.477121 0.477121 0.000000 0.60206 0.477121
add a comment |
You may want to use the applymap
method to apply math.log10
on the whole dataframe, here is the documentation.
You can test it:
df.applymap(math.log10)
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%2f55024529%2fhow-to-use-math-log10-function-on-whole-pandas-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the numpy version, not math
import numpy as np
np.log10(df)
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Use the numpy version, not math
import numpy as np
np.log10(df)
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Use the numpy version, not math
import numpy as np
np.log10(df)
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Use the numpy version, not math
import numpy as np
np.log10(df)
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 1 hour ago
ecortazarecortazar
1713
1713
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
ecortazar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
From what it seems math.log10
cannot handle neither dataframes nor ndarrays
.
So you can either go with np.log10
and reconstruct the dataframe as pointed out in other solutions, or if you want to go with math.log10
, you can use DataFrame.applymap
to apply math.log10
to the dataframe elementwise. Note however that this solution will be slower than a vectorized approach using np.log10
.
df.applymap(math.log10)
Example
df = pd.DataFrame(np.random.randint(1,5,(6,6)), columns=list('abcdef'))
print(df)
a b c d e f
0 3 4 1 1 2 1
1 4 4 4 3 4 1
2 4 3 3 1 4 1
3 3 4 1 3 1 1
4 1 2 3 4 2 1
5 1 3 3 1 4 3
df.applymap(math.log10)
a b c d e f
0 0.477121 0.602060 0.000000 0.000000 0.30103 0.000000
1 0.602060 0.602060 0.602060 0.477121 0.60206 0.000000
2 0.602060 0.477121 0.477121 0.000000 0.60206 0.000000
3 0.477121 0.602060 0.000000 0.477121 0.00000 0.000000
4 0.000000 0.301030 0.477121 0.602060 0.30103 0.000000
5 0.000000 0.477121 0.477121 0.000000 0.60206 0.477121
add a comment |
From what it seems math.log10
cannot handle neither dataframes nor ndarrays
.
So you can either go with np.log10
and reconstruct the dataframe as pointed out in other solutions, or if you want to go with math.log10
, you can use DataFrame.applymap
to apply math.log10
to the dataframe elementwise. Note however that this solution will be slower than a vectorized approach using np.log10
.
df.applymap(math.log10)
Example
df = pd.DataFrame(np.random.randint(1,5,(6,6)), columns=list('abcdef'))
print(df)
a b c d e f
0 3 4 1 1 2 1
1 4 4 4 3 4 1
2 4 3 3 1 4 1
3 3 4 1 3 1 1
4 1 2 3 4 2 1
5 1 3 3 1 4 3
df.applymap(math.log10)
a b c d e f
0 0.477121 0.602060 0.000000 0.000000 0.30103 0.000000
1 0.602060 0.602060 0.602060 0.477121 0.60206 0.000000
2 0.602060 0.477121 0.477121 0.000000 0.60206 0.000000
3 0.477121 0.602060 0.000000 0.477121 0.00000 0.000000
4 0.000000 0.301030 0.477121 0.602060 0.30103 0.000000
5 0.000000 0.477121 0.477121 0.000000 0.60206 0.477121
add a comment |
From what it seems math.log10
cannot handle neither dataframes nor ndarrays
.
So you can either go with np.log10
and reconstruct the dataframe as pointed out in other solutions, or if you want to go with math.log10
, you can use DataFrame.applymap
to apply math.log10
to the dataframe elementwise. Note however that this solution will be slower than a vectorized approach using np.log10
.
df.applymap(math.log10)
Example
df = pd.DataFrame(np.random.randint(1,5,(6,6)), columns=list('abcdef'))
print(df)
a b c d e f
0 3 4 1 1 2 1
1 4 4 4 3 4 1
2 4 3 3 1 4 1
3 3 4 1 3 1 1
4 1 2 3 4 2 1
5 1 3 3 1 4 3
df.applymap(math.log10)
a b c d e f
0 0.477121 0.602060 0.000000 0.000000 0.30103 0.000000
1 0.602060 0.602060 0.602060 0.477121 0.60206 0.000000
2 0.602060 0.477121 0.477121 0.000000 0.60206 0.000000
3 0.477121 0.602060 0.000000 0.477121 0.00000 0.000000
4 0.000000 0.301030 0.477121 0.602060 0.30103 0.000000
5 0.000000 0.477121 0.477121 0.000000 0.60206 0.477121
From what it seems math.log10
cannot handle neither dataframes nor ndarrays
.
So you can either go with np.log10
and reconstruct the dataframe as pointed out in other solutions, or if you want to go with math.log10
, you can use DataFrame.applymap
to apply math.log10
to the dataframe elementwise. Note however that this solution will be slower than a vectorized approach using np.log10
.
df.applymap(math.log10)
Example
df = pd.DataFrame(np.random.randint(1,5,(6,6)), columns=list('abcdef'))
print(df)
a b c d e f
0 3 4 1 1 2 1
1 4 4 4 3 4 1
2 4 3 3 1 4 1
3 3 4 1 3 1 1
4 1 2 3 4 2 1
5 1 3 3 1 4 3
df.applymap(math.log10)
a b c d e f
0 0.477121 0.602060 0.000000 0.000000 0.30103 0.000000
1 0.602060 0.602060 0.602060 0.477121 0.60206 0.000000
2 0.602060 0.477121 0.477121 0.000000 0.60206 0.000000
3 0.477121 0.602060 0.000000 0.477121 0.00000 0.000000
4 0.000000 0.301030 0.477121 0.602060 0.30103 0.000000
5 0.000000 0.477121 0.477121 0.000000 0.60206 0.477121
edited 31 mins ago
answered 1 hour ago


yatuyatu
12.3k31340
12.3k31340
add a comment |
add a comment |
You may want to use the applymap
method to apply math.log10
on the whole dataframe, here is the documentation.
You can test it:
df.applymap(math.log10)
add a comment |
You may want to use the applymap
method to apply math.log10
on the whole dataframe, here is the documentation.
You can test it:
df.applymap(math.log10)
add a comment |
You may want to use the applymap
method to apply math.log10
on the whole dataframe, here is the documentation.
You can test it:
df.applymap(math.log10)
You may want to use the applymap
method to apply math.log10
on the whole dataframe, here is the documentation.
You can test it:
df.applymap(math.log10)
edited 53 mins ago
IanS
8,56232763
8,56232763
answered 1 hour ago
Valentin MercierValentin Mercier
8710
8710
add a comment |
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%2f55024529%2fhow-to-use-math-log10-function-on-whole-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