Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat a DataFrame with a Series in Pandas

Tags:

python

pandas

Can someone explain what is wrong with this pandas concat code, and why data frame remains empty ?I am using anaconda distibution, and as far as I remember it was working before. enter image description here

like image 630
cacert Avatar asked Mar 06 '16 18:03

cacert


People also ask

Can you append a series to a DataFrame?

Pandas DataFrame. append() will append rows (add rows) of other DataFrame, Series, Dictionary or list of these to another DataFrame.

How do I combine two DataFrames in Pandas?

When we concatenate DataFrames, we need to specify the axis. axis=0 tells pandas to stack the second DataFrame UNDER the first one. It will automatically detect whether the column names are the same and will stack accordingly. axis=1 will stack the columns in the second DataFrame to the RIGHT of the first DataFrame.


1 Answers

You want to use this form:

result = pd.concat([dataframe, series], axis=1)

The pd.concat(...) doesn't happen "inplace" into the original dataframe but it would return the concatenated result so you'll want to assign the concatenation somewhere, e.g.:

>>> import pandas as pd
>>> s = pd.Series([1,2,3])
>>> df = pd.DataFrame()
>>> df = pd.concat([df, s], axis=1)  # We assign the result back into df
>>> df
   0
0  1
1  2
2  3
like image 194
bakkal Avatar answered Oct 19 '22 23:10

bakkal