How to perform mathematical operations on all CSV file columns & rows using Pandas
Here is my code:
all_data = pd.read_csv('data2.csv')
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
print(all_data.shape)
print(mu.shape)
print(sigma.shape)
Output:
(20, 24)
(1, 24)
(1, 24)
Sigma and Mu are numpy array or matrix.
I want to perform this operaiton:
all_data = (all_data - mu)/sigma
Here, first column (all rows), of all_data
first gets substracted by first column of mu
and then divided by first column of sigma
second column (all rows) of all_data
first gets substracted by second column of mu
and then divided by second column of sigma
Like that
python pandas numpy
add a comment |
Here is my code:
all_data = pd.read_csv('data2.csv')
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
print(all_data.shape)
print(mu.shape)
print(sigma.shape)
Output:
(20, 24)
(1, 24)
(1, 24)
Sigma and Mu are numpy array or matrix.
I want to perform this operaiton:
all_data = (all_data - mu)/sigma
Here, first column (all rows), of all_data
first gets substracted by first column of mu
and then divided by first column of sigma
second column (all rows) of all_data
first gets substracted by second column of mu
and then divided by second column of sigma
Like that
python pandas numpy
add a comment |
Here is my code:
all_data = pd.read_csv('data2.csv')
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
print(all_data.shape)
print(mu.shape)
print(sigma.shape)
Output:
(20, 24)
(1, 24)
(1, 24)
Sigma and Mu are numpy array or matrix.
I want to perform this operaiton:
all_data = (all_data - mu)/sigma
Here, first column (all rows), of all_data
first gets substracted by first column of mu
and then divided by first column of sigma
second column (all rows) of all_data
first gets substracted by second column of mu
and then divided by second column of sigma
Like that
python pandas numpy
Here is my code:
all_data = pd.read_csv('data2.csv')
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
print(all_data.shape)
print(mu.shape)
print(sigma.shape)
Output:
(20, 24)
(1, 24)
(1, 24)
Sigma and Mu are numpy array or matrix.
I want to perform this operaiton:
all_data = (all_data - mu)/sigma
Here, first column (all rows), of all_data
first gets substracted by first column of mu
and then divided by first column of sigma
second column (all rows) of all_data
first gets substracted by second column of mu
and then divided by second column of sigma
Like that
python pandas numpy
python pandas numpy
asked Nov 19 '18 at 15:59
john doe
203
203
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:
(all_data.values - mu)/sigma
And put it back in a dataframe with the same columns/index as all_data
:
pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
Example:
On this mini dataframe:
all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
>>> all_data
0 1 2 3 4
0 5 7 1 8 6
1 5 8 0 3 0
2 8 2 0 1 6
3 5 8 7 7 0
4 4 6 0 2 5
With:
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
>>> mu
array([[5.6, 2. , 4. , 4.4, 7.6]])
>>> sigma
array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])
You can get:
>>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
0 1 2 3 4
0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
3 -0.369274 4.743416 0.88083 1.075888 -15.513435
4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228
Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma
and mu
, second column with second values, etc...
add a comment |
how about numpy.matlib.repmat
?
df = pd.DataFrame(numpy.random.rand(20, 24))
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
all_data = (all_data - MU)/SIGMA
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%2f53378420%2fhow-to-perform-mathematical-operations-on-all-csv-file-columns-rows-using-pand%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
If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:
(all_data.values - mu)/sigma
And put it back in a dataframe with the same columns/index as all_data
:
pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
Example:
On this mini dataframe:
all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
>>> all_data
0 1 2 3 4
0 5 7 1 8 6
1 5 8 0 3 0
2 8 2 0 1 6
3 5 8 7 7 0
4 4 6 0 2 5
With:
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
>>> mu
array([[5.6, 2. , 4. , 4.4, 7.6]])
>>> sigma
array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])
You can get:
>>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
0 1 2 3 4
0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
3 -0.369274 4.743416 0.88083 1.075888 -15.513435
4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228
Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma
and mu
, second column with second values, etc...
add a comment |
If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:
(all_data.values - mu)/sigma
And put it back in a dataframe with the same columns/index as all_data
:
pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
Example:
On this mini dataframe:
all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
>>> all_data
0 1 2 3 4
0 5 7 1 8 6
1 5 8 0 3 0
2 8 2 0 1 6
3 5 8 7 7 0
4 4 6 0 2 5
With:
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
>>> mu
array([[5.6, 2. , 4. , 4.4, 7.6]])
>>> sigma
array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])
You can get:
>>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
0 1 2 3 4
0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
3 -0.369274 4.743416 0.88083 1.075888 -15.513435
4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228
Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma
and mu
, second column with second values, etc...
add a comment |
If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:
(all_data.values - mu)/sigma
And put it back in a dataframe with the same columns/index as all_data
:
pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
Example:
On this mini dataframe:
all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
>>> all_data
0 1 2 3 4
0 5 7 1 8 6
1 5 8 0 3 0
2 8 2 0 1 6
3 5 8 7 7 0
4 4 6 0 2 5
With:
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
>>> mu
array([[5.6, 2. , 4. , 4.4, 7.6]])
>>> sigma
array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])
You can get:
>>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
0 1 2 3 4
0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
3 -0.369274 4.743416 0.88083 1.075888 -15.513435
4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228
Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma
and mu
, second column with second values, etc...
If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:
(all_data.values - mu)/sigma
And put it back in a dataframe with the same columns/index as all_data
:
pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
Example:
On this mini dataframe:
all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
>>> all_data
0 1 2 3 4
0 5 7 1 8 6
1 5 8 0 3 0
2 8 2 0 1 6
3 5 8 7 7 0
4 4 6 0 2 5
With:
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
>>> mu
array([[5.6, 2. , 4. , 4.4, 7.6]])
>>> sigma
array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])
You can get:
>>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
0 1 2 3 4
0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
3 -0.369274 4.743416 0.88083 1.075888 -15.513435
4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228
Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma
and mu
, second column with second values, etc...
answered Nov 19 '18 at 16:29


sacul
29.9k41740
29.9k41740
add a comment |
add a comment |
how about numpy.matlib.repmat
?
df = pd.DataFrame(numpy.random.rand(20, 24))
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
all_data = (all_data - MU)/SIGMA
add a comment |
how about numpy.matlib.repmat
?
df = pd.DataFrame(numpy.random.rand(20, 24))
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
all_data = (all_data - MU)/SIGMA
add a comment |
how about numpy.matlib.repmat
?
df = pd.DataFrame(numpy.random.rand(20, 24))
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
all_data = (all_data - MU)/SIGMA
how about numpy.matlib.repmat
?
df = pd.DataFrame(numpy.random.rand(20, 24))
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
all_data = (all_data - MU)/SIGMA
answered Nov 19 '18 at 16:35
Robert
33429
33429
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.
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%2f53378420%2fhow-to-perform-mathematical-operations-on-all-csv-file-columns-rows-using-pand%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