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.
pandas dataframe
|
show 1 more comment
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.
pandas dataframe
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 that4.0
may become int as literal4
and0.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
|
show 1 more comment
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.
pandas dataframe
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
pandas dataframe
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 that4.0
may become int as literal4
and0.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
|
show 1 more comment
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 that4.0
may become int as literal4
and0.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
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53232328%2fpandas-creating-dataframe-with-fixed-dtypes%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
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 literal4
and0.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