How to combine two rows in dataframe pandas

I have a dataframe with two rows, and I would like to combine two rows into one row. Df Looks like this:

PC Rating CY Rating PY HT 0 DE101 NaN AA GV 0 DE101 AA+ NaN GV 

I tried to create two separate data frames and combine them with df.merge (df2) without success. The result should be as follows

  PC Rating CY Rating PY HT 0 DE101 AA+ AA GV 

Any ideas? thanks in advance Can df.update be a possible solution?

EDIT:

 df.head(1).combine_first(df.tail(1)) 

This works for the example above. However, for columns containing numerical values, this approach does not give the desired result, for example. for

  PC Rating CY Rating PY HT MV1 MV2 0 DE101 NaN AA GV 0 20 0 DE101 AA+ NaN GV 10 0 

The output should be:

  PC Rating CY Rating PY HT MV1 MV2 0 DE101 AA+ AA GV 10 20 

The above formula does not summarize the values ​​in the last two columns, but takes the values ​​in the first row of the data block.

  PC Rating CY Rating PY HT MV1 MV2 0 DE101 AA+ AA GV 0 20 

How can I fix this problem?

+6
source share
2 answers

You can use the DF.combine_first() method after dividing DF into 2 parts, where the zero values ​​in the first half will be replaced by the final values ​​in the other half, while keeping the other final values ​​intact:

 df.head(1).combine_first(df.tail(1)) # Practically this is same as β†’ df.head(1).fillna(df.tail(1)) 

enter image description here


If there were columns of a mixed data type, dividing them into component dtype columns, and then performing various operations on it would be feasible by chaining them.

 obj_df = df.select_dtypes(include=[np.object]) num_df = df.select_dtypes(exclude=[np.object]) obj_df.head(1).combine_first(obj_df.tail(1)).join(num_df.head(1).add(num_df.tail(1))) 

enter image description here

+4
source

You can use max with transpose, for example

 In [2103]: df.max().to_frame().T Out[2103]: PC Rating CY Rating PY HT MV1 MV2 0 DE101 AA+ AA GV 10 20 
+3
source

Source: https://habr.com/ru/post/1262913/


All Articles