Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat series onto dataframe with column name

I want to add a Series (s) to a Pandas DataFrame (df) as a new column. The series has more values than there are rows in the dataframe, so I am using the concat method along axis 1.

df = pd.concat((df, s), axis=1)

This works, but the new column of the dataframe representing the series is given an arbitrary numerical column name, and I would like this column to have a specific name instead.

Is there a way to add a series to a dataframe, when the series is longer than the rows of the dataframe, and with a specified column name in the resulting dataframe?

like image 925
vaer-k Avatar asked Aug 19 '16 21:08

vaer-k


People also ask

How do you concatenate a series?

Select the entire formula and press F9 (this converts the formula into values). Remove the curly brackets from both ends. Add =CONCATENATE( to the beginning of the text and end it with a round bracket). Press Enter.

How do I merge data frames with different column names?

Different column names are specified for merges in Pandas using the “left_on” and “right_on” parameters, instead of using only the “on” parameter. Merging dataframes with different names for the joining variable is achieved using the left_on and right_on arguments to the pandas merge function.

Can you append series to DataFrame?

Appending a multiple rows - Appending a list of Dictionaries to a DataFrame. You can also pass a list of Series or a list of Dictionaries to append multiple rows.

How to concatenate two series in pandas?

There are several ways to concatenate two series in pandas. Following are some of the ways: Method 1:Usingpandas.concat(). This method does all of the heavy liftingof performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.

How do I join two pandas DataFrames?

Pandas have high performance in-memory join operations which is very similar to RDBMS like SQL. merge can be used for all database join operations between dataframe or named series objects. You have to pass an extra parameter “name” to the series in this case. Method 4: Using Dataframe.join ().

Is there a way to add a series to a Dataframe?

Is there a way to add a series to a dataframe, when the series is longer than the rows of the dataframe, and with a specified column name in the resulting dataframe? One option is simply to specify the name when creating the series: Using the name attribute when creating the series is all I needed.

How do I merge DataFrames in Python?

Merge DataFrames by indexes or columns. The keys, levels, and names arguments are all optional. A walkthrough of how this method fits in with other tools for combining pandas objects can be found here. Combine two Series. Clear the existing index and reset it in the result by setting the ignore_index option to True.


1 Answers

You can try Series.rename:

df = pd.concat((df, s.rename('col')), axis=1) 
like image 62
jezrael Avatar answered Sep 28 '22 08:09

jezrael