4

I'm trying to merge two series into one single row in a new dataframe and then add more rows into it. For example, I have series as below:

s1

var1       10
var2        9
var3        2

s2
var4        1
var5       81
var6       13

I want to make them into a dataframe like this:

    df
    var1  var2  var3  var4  var5  var6
 0    10     9     2     1    81    13

The closest solution I can find is this: How to append two series into a dataframe Row wise

However if we do this we'll get something like this:

result = pd.concat([s1, s2], axis=1).T
print(result)

    result

    var1  var2  var3  var4  var5  var6
 0    10     9     2   NaN   NaN   NaN
 1   NaN   NaN   NaN     1    81    13

which is clearly not what we want.

Thanks!

2 Answers 2

4

alternative solution:

In [72]: pd.concat([s1,s2]).to_frame().T
Out[72]:
   var1  var2  var3  var4  var5  var6
0    10     9     2     1    81    13
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Works perfectly! It took me hours searching on the internet and couldn't find a solution. Thank you so much!!!
2

Try this instead:

Setup

s1 = pd.Series([10, 9, 2], ['var1', 'var2', 'var3'])
s2 = pd.Series([1, 81, 13], ['var4', 'var5', 'var6'])

pd.DataFrame(pd.concat([s1, s2])).T

   var1  var2  var3  var4  var5  var6
0    10     9     2     1    81    13

1 Comment

Works! Thanks! I'm still pretty new to pandas. The difference between dataframes and series really confuses me sometimes. I think your example gives me some ideas about how it works. Thanks a million!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.