replacing a pandas dataframe row overwrites all columns' dtypes

fantabolous

When I replace a row of a df, it causes an existing column of dtype=int to become float. I would like to keep it as int.

I create the df:

testdate = pd.datetime(2014, 1, 1)
adddata = {'intcol':0,'floatcol':0.0}
df = pd.DataFrame(data=adddata, index=pd.date_range(testdate, periods=1))

As desired, one column is int and the other is float, as confirmed by df.dtypes:

floatcol    float64
intcol        int64
dtype: object

Then I overwrite an existing row (in this case there's only 1) using df.ix[testdate] = pd.Series(adddata). I purposely use the same data to show the issue: the intcol has become float. df.dtypes:

floatcol    float64
intcol      float64
dtype: object

Note that I can change the cells individually (e.g. df.ix[testdate,'floatcol'] = 0.0) and the column dtypes are maintained, but in reality I have far more than 2 columns that I want to overwrite simultaneously so doing them one at a time is cumbersome.

behzad.nouri

interesting that even specifying the data type as object does not help:

>>> df.loc[testdate,:] = pd.Series(adddata, dtype='object')
>>> df.dtypes
floatcol    float64
intcol      float64
dtype: object

someone may have a better solution, but i noticed that this works:

>>> df.loc[testdate,:] = pd.Series(list(adddata.values()), adddata.keys(), dtype='object')
>>> df.dtypes
floatcol    float64
intcol        int64
dtype: object

but, if the row values are in dict format, probably this would be easier:

>>> df.loc[testdate,:] = list(map(adddata.get, df.columns))
>>> df.dtypes
floatcol    float64
intcol        int64
dtype: object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Pandas: dtypes not show column types for all columns

From Dev

pandas dataframe how to sum all value of bigger columns per row

From Dev

automatic encoding of all strings in a pandas dataframe.dtypes

From Dev

Replacing values in pandas dataframe columns by criteria

From Dev

Pandas: Replacing column values in dataframe columns

From Dev

Pandas Dataframe: Replacing NaN with row average

From Dev

Replacing Columns from one dataframe with columns from another dataframe in pandas

From Dev

Replacing Columns from one dataframe with columns from another dataframe in pandas

From Dev

Pandas Converting columns to different dtypes

From Java

Assign pandas dataframe column dtypes

From Java

what are all the dtypes that pandas recognizes?

From Dev

shifting all the columns in a dataframe to extreme end replacing all nan

From Dev

Concatenate all columns in a pandas dataframe

From Dev

Pandas Dataframe: Expand rows with lists to multiple row with desired indexing for all columns

From Dev

Transpose/Pivot DataFrame but not all columns in the same row

From Dev

Pandas dataframe: Is there a difference in performance in replacing values by column and row?

From Dev

Converting tuples in a row to a new columns in pandas Dataframe

From Dev

Set values for particular columns in a row of a Pandas Dataframe

From Dev

pandas dataframe column based on row and multiple columns

From Dev

pandas unique values multiple columns different dtypes

From Dev

Replacing Values in Pandas Columns

From Dev

Replacing row values in pandas

From Dev

How to set dtypes by column in pandas DataFrame

From Java

How to show all of columns name on pandas dataframe?

From Dev

Search for String in all Pandas DataFrame columns and filter

From Java

pandas how to check dtype for all columns in a dataframe?

From Dev

Not calculating sum for all columns in pandas dataframe

From Dev

Pandas: Find the maximum range in all the columns of dataframe

From Dev

pandas select rows by condition for all of dataframe columns

Related Related

  1. 1

    Python Pandas: dtypes not show column types for all columns

  2. 2

    pandas dataframe how to sum all value of bigger columns per row

  3. 3

    automatic encoding of all strings in a pandas dataframe.dtypes

  4. 4

    Replacing values in pandas dataframe columns by criteria

  5. 5

    Pandas: Replacing column values in dataframe columns

  6. 6

    Pandas Dataframe: Replacing NaN with row average

  7. 7

    Replacing Columns from one dataframe with columns from another dataframe in pandas

  8. 8

    Replacing Columns from one dataframe with columns from another dataframe in pandas

  9. 9

    Pandas Converting columns to different dtypes

  10. 10

    Assign pandas dataframe column dtypes

  11. 11

    what are all the dtypes that pandas recognizes?

  12. 12

    shifting all the columns in a dataframe to extreme end replacing all nan

  13. 13

    Concatenate all columns in a pandas dataframe

  14. 14

    Pandas Dataframe: Expand rows with lists to multiple row with desired indexing for all columns

  15. 15

    Transpose/Pivot DataFrame but not all columns in the same row

  16. 16

    Pandas dataframe: Is there a difference in performance in replacing values by column and row?

  17. 17

    Converting tuples in a row to a new columns in pandas Dataframe

  18. 18

    Set values for particular columns in a row of a Pandas Dataframe

  19. 19

    pandas dataframe column based on row and multiple columns

  20. 20

    pandas unique values multiple columns different dtypes

  21. 21

    Replacing Values in Pandas Columns

  22. 22

    Replacing row values in pandas

  23. 23

    How to set dtypes by column in pandas DataFrame

  24. 24

    How to show all of columns name on pandas dataframe?

  25. 25

    Search for String in all Pandas DataFrame columns and filter

  26. 26

    pandas how to check dtype for all columns in a dataframe?

  27. 27

    Not calculating sum for all columns in pandas dataframe

  28. 28

    Pandas: Find the maximum range in all the columns of dataframe

  29. 29

    pandas select rows by condition for all of dataframe columns

HotTag

Archive