'When concatenating DataFrame and Series, the series is inserted in "vertical"

I'm iterating over a DataFrame with DataFrame.iterrows():

for index, row_to_append in source_dataframe.iterrows():

Then I want to add some of the rows to some other DataFrames. For the appending I'm using:

pd.concat([destination_dataframe, row_to_append], axis=0, join='outer', ignore_index=True)

But the concatenation works in the wrong way, in fact given the destination_dataframe as:

   0         1         2         3  ...         5         6         7          class
0  0.470588  0.896774  0.408163  0.239130  ...  0.104294  0.253629  0.183333    yes
1  0.000000  0.600000  0.163265  0.304348  ...  0.509202  0.943638  0.200000    yes
2  0.176471  0.219355  0.265306  0.271739  ...  0.261759  0.072588  0.083333    yes
3  0.117647  0.987097  0.469388  0.413043  ...  0.251534  0.034159  0.533333    yes
4  0.058824  0.264516  0.428571  0.239130  ...  0.171779  0.116567  0.166667     no
5  0.058824  0.290323  0.428571  0.173913  ...  0.202454  0.038002  0.000000     no
6  0.294118  0.464516  0.510204  0.239130  ...  0.151329  0.052519  0.150000     no

And the row_to_add:

(0, 0.411765) (1, 0.36129) (2, 0.489796) (3, 0.23913) (4, 0.169471) (5, 0.241309) (6, 0.173356) (7, 0.183333) ('class', 'yes')

The output of the concatenation is:

       0         1         2  ...         6         7           class
0      0.470588  0.896774  0.408163  ...  0.253629  0.183333    yes
1           0.0  0.600000  0.163265  ...  0.943638  0.200000    yes
2      0.176471  0.219355  0.265306  ...  0.072588  0.083333    yes
3      0.117647  0.987097  0.469388  ...  0.034159  0.533333    yes
4      0.058824  0.264516  0.428571  ...  0.116567  0.166667     no
5      0.058824  0.290323  0.428571  ...  0.038002  0.000000     no
6      0.294118  0.464516  0.510204  ...  0.052519  0.150000     no
0      0.411765       NaN       NaN  ...       NaN       NaN    NaN
1       0.36129       NaN       NaN  ...       NaN       NaN    NaN
2      0.489796       NaN       NaN  ...       NaN       NaN    NaN
3       0.23913       NaN       NaN  ...       NaN       NaN    NaN
4      0.169471       NaN       NaN  ...       NaN       NaN    NaN
5      0.241309       NaN       NaN  ...       NaN       NaN    NaN
6      0.173356       NaN       NaN  ...       NaN       NaN    NaN
7      0.183333       NaN       NaN  ...       NaN       NaN    NaN
class       yes       NaN       NaN  ...       NaN       NaN    NaN

Where the row gets added "vertically" and not horizontally.

While I want:

       0         1         2  ...         6         7           class
0      0.470588  0.896774  0.408163  ...  0.253629  0.183333    yes
1           0.0  0.600000  0.163265  ...  0.943638  0.200000    yes
2      0.176471  0.219355  0.265306  ...  0.072588  0.083333    yes
3      0.117647  0.987097  0.469388  ...  0.034159  0.533333    yes
4      0.058824  0.264516  0.428571  ...  0.116567  0.166667     no
5      0.058824  0.290323  0.428571  ...  0.038002  0.000000     no
6      0.294118  0.464516  0.510204  ...  0.052519  0.150000     no
7      0.411765  0.36129   0.489796  ...  0.173356  0.183333    yes <-------

I tried changing the axis, join and ignore_index parameters but still didn't get the result.



Solution 1:[1]

You can convert the row_to_append to dataframe

row_to_append  = pd.Series(row_to_append).to_frame().T

out = pd.concat([destination_dataframe, row_to_append], axis=0, join='outer', ignore_index=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BENY