简体   繁体   中英

Dataframe: copy one row into another while keeping different dtypes of columns

I have a dataframe with columns of different datatypes (floats and ints). Two rows are in the wrong order and I need to swap them, but copying a row onto another does not work.

import pandas as pd

df = pd.DataFrame([
        {"a":2.5, "b":10},
        {"a":2.7, "b":12},
        {"a":2.8, "b":16},
        {"a":3.1, "b":18}
    ])

This does copy the values, but afterwards all rows are of type 'float' (Series objects only have a single datatype.):

df.iloc[1] = df.iloc[2].copy()  # changes datatype if b to float

Copying the rows by using slices sets the whole row to NaN:

df.iloc[1:2] = df.iloc[2:3].copy()  # sets row 1 to NaN,NaN

a   b
0   2.5 10.0
1   NaN NaN
2   2.8 16.0
3   3.1 18.0

2 Questions:

  • whats happening in the second case, where do the NaN s come from?
  • how do I copy a row onto another row while keeping the datatypes?

whats happening in the second case, where do the NaNs come from?

Problem is different index values of sliced DataFrames, pandas cannot align rows, so NaNs are created:

print (df.iloc[1:2])
     a   b
1  2.7  12

print (df.iloc[2:3])
     a   b
2  2.8  16

how do I copy a row onto another row while keeping the datatypes?

One solution is create one row DataFrame and change index name for alignment:

df.iloc[[1]] = df.iloc[[2]].rename(index={2:1}).copy()

More general if need indexing index values:

df.iloc[[1]] = df.iloc[[2]].rename(index={df.index[2]: df.index[1]}).copy()
print (df)

0  2.5  10
1  2.8  16
2  2.8  16
3  3.1  18

Converting to numpy array is posible, but then dtypes are changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM