Why is DataFrame changing the datatype of all input into objects?
I pass numpy array and pd.Series with different data types, text, int, and floats into pd.DataFrame and the output is a DataFrame with all object datatypes. Why does it do that and is there anything I can do to preserve the original datatypes?
pd.DataFrame(np.c_[X, TotalSF, TotalBaths, HasFire], columns=(list(X.columns) + ['TotalSF', 'TotalBaths', 'HasFire']))
X is a 2-D array with some values as text and some as number.
TotalSF, TotalBaths, and HasFire are pandas Series with numbers as values.
python pandas numpy dataframe series
add a comment |
I pass numpy array and pd.Series with different data types, text, int, and floats into pd.DataFrame and the output is a DataFrame with all object datatypes. Why does it do that and is there anything I can do to preserve the original datatypes?
pd.DataFrame(np.c_[X, TotalSF, TotalBaths, HasFire], columns=(list(X.columns) + ['TotalSF', 'TotalBaths', 'HasFire']))
X is a 2-D array with some values as text and some as number.
TotalSF, TotalBaths, and HasFire are pandas Series with numbers as values.
python pandas numpy dataframe series
2
Note that the datatype of a dataframe column (or series) will only be float if all rows in the column or series are floats. Similar for integer dtypes. Basically if even one row of a series is a string/object then the datatype of the whole column will be object.
– JohnE
Nov 24 '18 at 1:15
add a comment |
I pass numpy array and pd.Series with different data types, text, int, and floats into pd.DataFrame and the output is a DataFrame with all object datatypes. Why does it do that and is there anything I can do to preserve the original datatypes?
pd.DataFrame(np.c_[X, TotalSF, TotalBaths, HasFire], columns=(list(X.columns) + ['TotalSF', 'TotalBaths', 'HasFire']))
X is a 2-D array with some values as text and some as number.
TotalSF, TotalBaths, and HasFire are pandas Series with numbers as values.
python pandas numpy dataframe series
I pass numpy array and pd.Series with different data types, text, int, and floats into pd.DataFrame and the output is a DataFrame with all object datatypes. Why does it do that and is there anything I can do to preserve the original datatypes?
pd.DataFrame(np.c_[X, TotalSF, TotalBaths, HasFire], columns=(list(X.columns) + ['TotalSF', 'TotalBaths', 'HasFire']))
X is a 2-D array with some values as text and some as number.
TotalSF, TotalBaths, and HasFire are pandas Series with numbers as values.
python pandas numpy dataframe series
python pandas numpy dataframe series
edited Nov 24 '18 at 1:13
JohnE
14.2k53457
14.2k53457
asked Nov 23 '18 at 23:18
Youi RabiYoui Rabi
295
295
2
Note that the datatype of a dataframe column (or series) will only be float if all rows in the column or series are floats. Similar for integer dtypes. Basically if even one row of a series is a string/object then the datatype of the whole column will be object.
– JohnE
Nov 24 '18 at 1:15
add a comment |
2
Note that the datatype of a dataframe column (or series) will only be float if all rows in the column or series are floats. Similar for integer dtypes. Basically if even one row of a series is a string/object then the datatype of the whole column will be object.
– JohnE
Nov 24 '18 at 1:15
2
2
Note that the datatype of a dataframe column (or series) will only be float if all rows in the column or series are floats. Similar for integer dtypes. Basically if even one row of a series is a string/object then the datatype of the whole column will be object.
– JohnE
Nov 24 '18 at 1:15
Note that the datatype of a dataframe column (or series) will only be float if all rows in the column or series are floats. Similar for integer dtypes. Basically if even one row of a series is a string/object then the datatype of the whole column will be object.
– JohnE
Nov 24 '18 at 1:15
add a comment |
1 Answer
1
active
oldest
votes
Dataframe workس with general datatypes, if you want to change your dataframe data type use
pandas.DataFrame.astype(target type)
track below code with and without astype method:
import pandas as pd
data = pd.DataFrame(data=[["red", "apple"], ["yellow", "orange"], ["blue", "banana"], ["green", "avocado"]],
columns=["color", "fruitN"])
# data = data.set_index("fruitN")
file_1 = ["akee", "apricot", "avocado"]
file_2 = ["avocado", "bilberry", "banana", "blackberry"]
file_3 = ["blackberry", "coconut", "cranberry"]
file_1_df = pd.DataFrame(data=file_1, columns=["type_1"])
file_2_df = pd.DataFrame(data=file_2, columns=["type_2"])
file_3_df = pd.DataFrame(data=file_3, columns=["type_3"])
l = [file_1_df, file_2_df, file_3_df]
for x, y in enumerate(l):
data['c' + str(x + 1)] = data.fruitN.isin(y.iloc[:, 0].tolist()).astype(int)
data = data["c2"].astype(int)
print(data)
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%2f53453798%2fwhy-is-dataframe-changing-the-datatype-of-all-input-into-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Dataframe workس with general datatypes, if you want to change your dataframe data type use
pandas.DataFrame.astype(target type)
track below code with and without astype method:
import pandas as pd
data = pd.DataFrame(data=[["red", "apple"], ["yellow", "orange"], ["blue", "banana"], ["green", "avocado"]],
columns=["color", "fruitN"])
# data = data.set_index("fruitN")
file_1 = ["akee", "apricot", "avocado"]
file_2 = ["avocado", "bilberry", "banana", "blackberry"]
file_3 = ["blackberry", "coconut", "cranberry"]
file_1_df = pd.DataFrame(data=file_1, columns=["type_1"])
file_2_df = pd.DataFrame(data=file_2, columns=["type_2"])
file_3_df = pd.DataFrame(data=file_3, columns=["type_3"])
l = [file_1_df, file_2_df, file_3_df]
for x, y in enumerate(l):
data['c' + str(x + 1)] = data.fruitN.isin(y.iloc[:, 0].tolist()).astype(int)
data = data["c2"].astype(int)
print(data)
add a comment |
Dataframe workس with general datatypes, if you want to change your dataframe data type use
pandas.DataFrame.astype(target type)
track below code with and without astype method:
import pandas as pd
data = pd.DataFrame(data=[["red", "apple"], ["yellow", "orange"], ["blue", "banana"], ["green", "avocado"]],
columns=["color", "fruitN"])
# data = data.set_index("fruitN")
file_1 = ["akee", "apricot", "avocado"]
file_2 = ["avocado", "bilberry", "banana", "blackberry"]
file_3 = ["blackberry", "coconut", "cranberry"]
file_1_df = pd.DataFrame(data=file_1, columns=["type_1"])
file_2_df = pd.DataFrame(data=file_2, columns=["type_2"])
file_3_df = pd.DataFrame(data=file_3, columns=["type_3"])
l = [file_1_df, file_2_df, file_3_df]
for x, y in enumerate(l):
data['c' + str(x + 1)] = data.fruitN.isin(y.iloc[:, 0].tolist()).astype(int)
data = data["c2"].astype(int)
print(data)
add a comment |
Dataframe workس with general datatypes, if you want to change your dataframe data type use
pandas.DataFrame.astype(target type)
track below code with and without astype method:
import pandas as pd
data = pd.DataFrame(data=[["red", "apple"], ["yellow", "orange"], ["blue", "banana"], ["green", "avocado"]],
columns=["color", "fruitN"])
# data = data.set_index("fruitN")
file_1 = ["akee", "apricot", "avocado"]
file_2 = ["avocado", "bilberry", "banana", "blackberry"]
file_3 = ["blackberry", "coconut", "cranberry"]
file_1_df = pd.DataFrame(data=file_1, columns=["type_1"])
file_2_df = pd.DataFrame(data=file_2, columns=["type_2"])
file_3_df = pd.DataFrame(data=file_3, columns=["type_3"])
l = [file_1_df, file_2_df, file_3_df]
for x, y in enumerate(l):
data['c' + str(x + 1)] = data.fruitN.isin(y.iloc[:, 0].tolist()).astype(int)
data = data["c2"].astype(int)
print(data)
Dataframe workس with general datatypes, if you want to change your dataframe data type use
pandas.DataFrame.astype(target type)
track below code with and without astype method:
import pandas as pd
data = pd.DataFrame(data=[["red", "apple"], ["yellow", "orange"], ["blue", "banana"], ["green", "avocado"]],
columns=["color", "fruitN"])
# data = data.set_index("fruitN")
file_1 = ["akee", "apricot", "avocado"]
file_2 = ["avocado", "bilberry", "banana", "blackberry"]
file_3 = ["blackberry", "coconut", "cranberry"]
file_1_df = pd.DataFrame(data=file_1, columns=["type_1"])
file_2_df = pd.DataFrame(data=file_2, columns=["type_2"])
file_3_df = pd.DataFrame(data=file_3, columns=["type_3"])
l = [file_1_df, file_2_df, file_3_df]
for x, y in enumerate(l):
data['c' + str(x + 1)] = data.fruitN.isin(y.iloc[:, 0].tolist()).astype(int)
data = data["c2"].astype(int)
print(data)
answered Nov 24 '18 at 0:04
saeed heidarisaeed heidari
1644
1644
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%2f53453798%2fwhy-is-dataframe-changing-the-datatype-of-all-input-into-objects%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
2
Note that the datatype of a dataframe column (or series) will only be float if all rows in the column or series are floats. Similar for integer dtypes. Basically if even one row of a series is a string/object then the datatype of the whole column will be object.
– JohnE
Nov 24 '18 at 1:15