pandas creating Dataframe with fixed dtypes









up vote
1
down vote

favorite












How to create DataFrame where columns are both int and float type proper..



I'm trying to create a sample pandas dataFrame while testing as follows but when I see the dtypes specilat in DataFrame df1 for column weight it converts int 4 to float as it has the second value .15



So, i'm not getting a way to create a column with mixed values like 4 as int and .15 as float.



$ df1 = pd.DataFrame( 'Matrial': ['gold', 'platnm'], 'weight': [4, .15])
$ df1
Matrial weight
0 gold 4.00
1 platnm 0.15


While checking dtypes..



$ df1.dtypes
Matrial object
weight float64
dtype: object


It should looks like:



$ df1
Matrial weight
0 gold 4
1 platnm .15


DataFrame df2 :



$ df2 = pd.DataFrame( 'ratio1': [8, 10], 'ratio2': [4, 2])
$ df2
ratio1 ratio2
0 8 4
1 10 2


While checking dtypes..



$ df2.dtypes
ratio1 int64
ratio2 int64
dtype: object


Sorry, question may be looks silly but being a new learner to pandas i'm not getting it.










share|improve this question



















  • 1




    You can't, the columns of a pandas DataFrame / Series are homogeneously of type.
    – Abhi
    Nov 9 at 19:55










  • @Abhi, so how to convert that in a way that 4.0 may become int as literal 4 and 0.15 may .15?
    – krock1516
    Nov 9 at 19:58











  • You can't convert a float to an int.
    – Abhi
    Nov 9 at 20:03











  • yes, df1's weight to df2's both column if its float then result will be altogether different which is in int
    – krock1516
    Nov 9 at 20:08






  • 1




    Your only option, if you must have the integer representation is to convert the entire column to an object type, which can be used as a container for multiple types. However, there really isn't any reason for this, and this should be avoided at all costs. You don't need to worry about precision, because small integers can be represented exactly with floats. Even with only 16 bits,np.float16(4) == int(4) evaluates to True. Using 64 bits you can represent enormous integers precisely.
    – ALollz
    Nov 9 at 20:28















up vote
1
down vote

favorite












How to create DataFrame where columns are both int and float type proper..



I'm trying to create a sample pandas dataFrame while testing as follows but when I see the dtypes specilat in DataFrame df1 for column weight it converts int 4 to float as it has the second value .15



So, i'm not getting a way to create a column with mixed values like 4 as int and .15 as float.



$ df1 = pd.DataFrame( 'Matrial': ['gold', 'platnm'], 'weight': [4, .15])
$ df1
Matrial weight
0 gold 4.00
1 platnm 0.15


While checking dtypes..



$ df1.dtypes
Matrial object
weight float64
dtype: object


It should looks like:



$ df1
Matrial weight
0 gold 4
1 platnm .15


DataFrame df2 :



$ df2 = pd.DataFrame( 'ratio1': [8, 10], 'ratio2': [4, 2])
$ df2
ratio1 ratio2
0 8 4
1 10 2


While checking dtypes..



$ df2.dtypes
ratio1 int64
ratio2 int64
dtype: object


Sorry, question may be looks silly but being a new learner to pandas i'm not getting it.










share|improve this question



















  • 1




    You can't, the columns of a pandas DataFrame / Series are homogeneously of type.
    – Abhi
    Nov 9 at 19:55










  • @Abhi, so how to convert that in a way that 4.0 may become int as literal 4 and 0.15 may .15?
    – krock1516
    Nov 9 at 19:58











  • You can't convert a float to an int.
    – Abhi
    Nov 9 at 20:03











  • yes, df1's weight to df2's both column if its float then result will be altogether different which is in int
    – krock1516
    Nov 9 at 20:08






  • 1




    Your only option, if you must have the integer representation is to convert the entire column to an object type, which can be used as a container for multiple types. However, there really isn't any reason for this, and this should be avoided at all costs. You don't need to worry about precision, because small integers can be represented exactly with floats. Even with only 16 bits,np.float16(4) == int(4) evaluates to True. Using 64 bits you can represent enormous integers precisely.
    – ALollz
    Nov 9 at 20:28













up vote
1
down vote

favorite









up vote
1
down vote

favorite











How to create DataFrame where columns are both int and float type proper..



I'm trying to create a sample pandas dataFrame while testing as follows but when I see the dtypes specilat in DataFrame df1 for column weight it converts int 4 to float as it has the second value .15



So, i'm not getting a way to create a column with mixed values like 4 as int and .15 as float.



$ df1 = pd.DataFrame( 'Matrial': ['gold', 'platnm'], 'weight': [4, .15])
$ df1
Matrial weight
0 gold 4.00
1 platnm 0.15


While checking dtypes..



$ df1.dtypes
Matrial object
weight float64
dtype: object


It should looks like:



$ df1
Matrial weight
0 gold 4
1 platnm .15


DataFrame df2 :



$ df2 = pd.DataFrame( 'ratio1': [8, 10], 'ratio2': [4, 2])
$ df2
ratio1 ratio2
0 8 4
1 10 2


While checking dtypes..



$ df2.dtypes
ratio1 int64
ratio2 int64
dtype: object


Sorry, question may be looks silly but being a new learner to pandas i'm not getting it.










share|improve this question















How to create DataFrame where columns are both int and float type proper..



I'm trying to create a sample pandas dataFrame while testing as follows but when I see the dtypes specilat in DataFrame df1 for column weight it converts int 4 to float as it has the second value .15



So, i'm not getting a way to create a column with mixed values like 4 as int and .15 as float.



$ df1 = pd.DataFrame( 'Matrial': ['gold', 'platnm'], 'weight': [4, .15])
$ df1
Matrial weight
0 gold 4.00
1 platnm 0.15


While checking dtypes..



$ df1.dtypes
Matrial object
weight float64
dtype: object


It should looks like:



$ df1
Matrial weight
0 gold 4
1 platnm .15


DataFrame df2 :



$ df2 = pd.DataFrame( 'ratio1': [8, 10], 'ratio2': [4, 2])
$ df2
ratio1 ratio2
0 8 4
1 10 2


While checking dtypes..



$ df2.dtypes
ratio1 int64
ratio2 int64
dtype: object


Sorry, question may be looks silly but being a new learner to pandas i'm not getting it.







pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 19:57

























asked Nov 9 at 19:43









krock1516

367213




367213







  • 1




    You can't, the columns of a pandas DataFrame / Series are homogeneously of type.
    – Abhi
    Nov 9 at 19:55










  • @Abhi, so how to convert that in a way that 4.0 may become int as literal 4 and 0.15 may .15?
    – krock1516
    Nov 9 at 19:58











  • You can't convert a float to an int.
    – Abhi
    Nov 9 at 20:03











  • yes, df1's weight to df2's both column if its float then result will be altogether different which is in int
    – krock1516
    Nov 9 at 20:08






  • 1




    Your only option, if you must have the integer representation is to convert the entire column to an object type, which can be used as a container for multiple types. However, there really isn't any reason for this, and this should be avoided at all costs. You don't need to worry about precision, because small integers can be represented exactly with floats. Even with only 16 bits,np.float16(4) == int(4) evaluates to True. Using 64 bits you can represent enormous integers precisely.
    – ALollz
    Nov 9 at 20:28













  • 1




    You can't, the columns of a pandas DataFrame / Series are homogeneously of type.
    – Abhi
    Nov 9 at 19:55










  • @Abhi, so how to convert that in a way that 4.0 may become int as literal 4 and 0.15 may .15?
    – krock1516
    Nov 9 at 19:58











  • You can't convert a float to an int.
    – Abhi
    Nov 9 at 20:03











  • yes, df1's weight to df2's both column if its float then result will be altogether different which is in int
    – krock1516
    Nov 9 at 20:08






  • 1




    Your only option, if you must have the integer representation is to convert the entire column to an object type, which can be used as a container for multiple types. However, there really isn't any reason for this, and this should be avoided at all costs. You don't need to worry about precision, because small integers can be represented exactly with floats. Even with only 16 bits,np.float16(4) == int(4) evaluates to True. Using 64 bits you can represent enormous integers precisely.
    – ALollz
    Nov 9 at 20:28








1




1




You can't, the columns of a pandas DataFrame / Series are homogeneously of type.
– Abhi
Nov 9 at 19:55




You can't, the columns of a pandas DataFrame / Series are homogeneously of type.
– Abhi
Nov 9 at 19:55












@Abhi, so how to convert that in a way that 4.0 may become int as literal 4 and 0.15 may .15?
– krock1516
Nov 9 at 19:58





@Abhi, so how to convert that in a way that 4.0 may become int as literal 4 and 0.15 may .15?
– krock1516
Nov 9 at 19:58













You can't convert a float to an int.
– Abhi
Nov 9 at 20:03





You can't convert a float to an int.
– Abhi
Nov 9 at 20:03













yes, df1's weight to df2's both column if its float then result will be altogether different which is in int
– krock1516
Nov 9 at 20:08




yes, df1's weight to df2's both column if its float then result will be altogether different which is in int
– krock1516
Nov 9 at 20:08




1




1




Your only option, if you must have the integer representation is to convert the entire column to an object type, which can be used as a container for multiple types. However, there really isn't any reason for this, and this should be avoided at all costs. You don't need to worry about precision, because small integers can be represented exactly with floats. Even with only 16 bits,np.float16(4) == int(4) evaluates to True. Using 64 bits you can represent enormous integers precisely.
– ALollz
Nov 9 at 20:28





Your only option, if you must have the integer representation is to convert the entire column to an object type, which can be used as a container for multiple types. However, there really isn't any reason for this, and this should be avoided at all costs. You don't need to worry about precision, because small integers can be represented exactly with floats. Even with only 16 bits,np.float16(4) == int(4) evaluates to True. Using 64 bits you can represent enormous integers precisely.
– ALollz
Nov 9 at 20:28


















active

oldest

votes











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',
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
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232328%2fpandas-creating-dataframe-with-fixed-dtypes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232328%2fpandas-creating-dataframe-with-fixed-dtypes%23new-answer', 'question_page');

);

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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo