I'm trying to convert a pandas dataframe into a Series of tuples:
df = pd.DataFrame([[1,2,3.0],[3,4,5.0]])
0 (1, 2, 3.0)
1 (3, 4, 5.0)
dtype: object
However pandas seems to coerce my integer columns to floats.
import pandas as pd
df = pd.DataFrame([[1,2,3.0],[3,4,5]])
print(df)
print(df.dtypes)
print(df.apply(tuple,axis=1,reduce=False).apply(str))
0 1 2
0 1 2 3.0
1 3 4 5.0
0 int64
1 int64
2 float64
dtype: object
0 (1.0, 2.0, 3.0)
1 (3.0, 4.0, 5.0)
dtype: object
This question suggests using reduce=False
but this doesn't change anything for me.
Could someone explain why pandas is coercing the datatype somewhere along the way?
to_numeric() This method is used to convert the data type of the column to the numerical one. As a result, the float64 or int64 will be returned as the new data type of the column based on the values in the column.
The dtype specified can be a buil-in Python, numpy , or pandas dtype. Let's suppose we want to convert column A (which is currently a string of type object ) into a column holding integers. To do so, we simply need to call astype on the pandas DataFrame object and explicitly define the dtype we wish to cast the column.
In order to change the dtype of the given array object, we will use numpy. astype() function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.
You can use df. astype() with a dictionary for the columns you want to change with the corresponding dtype. Save this answer.
pandas.DataFrame.itertuples
to avoid forcing your ints to floats
pd.Series([*df.itertuples(index=False)])
0 (1, 2, 3.0)
1 (3, 4, 5.0)
dtype: object
zip
, map
, splat... magicpd.Series([*zip(*map(df.get, df))])
0 (1, 2, 3.0)
1 (3, 4, 5.0)
dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With