PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] Why does concat Series to DataFrame with index matching columns not work?

 May 12, 2022     append, concat, dataframe, pandas, python     No comments   

Issue

I want to append a Series to a DataFrame where Series's index matches DataFrame's columns using pd.concat, but it gives me surprises:

df = pd.DataFrame(columns=['a', 'b'])
sr = pd.Series(data=[1,2], index=['a', 'b'], name=1)
pd.concat([df, sr], axis=0)
Out[11]: 
     a    b    0
a  NaN  NaN  1.0
b  NaN  NaN  2.0

What I expected is of course:

df.append(sr)
Out[14]: 
   a  b
1  1  2

It really surprises me that pd.concat is not index-columns aware. So is it true that if I want to concat a Series as a new row to a DF, then I can only use df.append instead?


Solution

Need DataFrame from Series by to_frame and transpose:

a = pd.concat([df, sr.to_frame(1).T])
print (a)
   a  b
1  1  2

Detail:

print (sr.to_frame(1).T)
   a  b
1  1  2

Or use setting with enlargement:

df.loc[1] = sr
print (df)
   a  b
1  1  2


Answered By - jezrael
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing