Reputation: 111
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!
Upvotes: 4
Views: 3256
Reputation: 210942
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
Upvotes: 4
Reputation: 294508
Try this instead:
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
Upvotes: 2