Dataframe Merge Series

  1. How To Merge A Series And DataFrame - Intellipaat
  2. Dataframe Merge Index
  3. Dataframe Merge Series
  4. Dataframe Merge Series 1
  5. Cached

Merge DataFrame or named Series objects with a database-style join. The join is done on columns or indexes. If joining columns on columns, the DataFrame indexes will be ignored. Otherwise if joining indexes on indexes or indexes on a column or columns, the index will be passed on. Merge DataFrame or named Series objects with a database-style join. The join is done on columns or indexes. If joining columns on columns, the DataFrame indexes will be ignored. Otherwise if joining indexes on indexes or indexes on a column or columns, the index will be passed on. When performing a cross merge, no column specifications to merge on are allowed.

Question or problem about Python programming:

Best I can come up with is

Can anybody suggest better syntax / faster method?

My attempts:

and

EDIT The first two answers posted highlighted a problem with my question, so please use the following to construct df:

Dataframe

with the final result

How to solve the problem:

Solution 1:

You could construct a dataframe from the series and then merge with the dataframe.
So you specify the data as the values but multiply them by the length, set the columns to the index and set params for left_index and right_index to True:

EDIT for the situation where you want the index of your constructed df from the series to use the index of the df then you can do the following:

This assumes that the indices match the length.

Solution 2:

Update
From v0.24.0 onwards, you can merge on DataFrame and Series as long as the Series is named.

Nowadays, you can simply convert the Series to a DataFrame with to_frame(). So (if joining on index):

Solution 3:

How To Merge A Series And DataFrame - Intellipaat

Here’s one way:

To break down what happens here…

pd.DataFrame(s).T creates a one-row DataFrame from s which looks like this:

Next, join concatenates this new frame with df:

Lastly, the NaN values at index 1 are filled with the previous values in the column using fillna with the forward-fill (ffill) argument:

To avoid using fillna, it’s possible to use pd.concat to repeat the rows of the DataFrame constructed from s. In this case, the general solution is:

Here’s another solution to address the indexing challenge posed in the edited question:

s is transformed into a DataFrame by repeating the values and reshaping (specifying ‘Fortran’ order), and also passing in the appropriate column names and index. This new DataFrame is then joined to df.

Solution 4:

If I could suggest setting up your dataframes like this (auto-indexing):

then you can set up your s1 and s2 values thus (using shape() to return the number of rows from df):

Dataframe Merge Series

then the result you want is easy:

Dataframe Merge Index

Alternatively, just add the new values to your dataframe df:

Both return:

If you have another list of data (instead of just a single value to apply), and you know it is in the same sequence as df, eg:

then you can attach this in the same way:

returns:

Dataframe Merge Series

Solution 5:

You can easily set a pandas.DataFrame column to a constant. This constant can be an int such as in your example. If the column you specify isn’t in the df, then pandas will create a new column with the name you specify. So after your dataframe is constructed, (from your question):

You can just run:

You could write a loop or comprehension to make it do this for all the elements in a list of tuples, or keys and values in a dictionary depending on how you have your real data stored.

Solution 6:

Dataframe Merge Series 1

If df is a pandas.DataFrame then df['new_col']= Series list_object of length len(df) will add the or Series list_object as a column named 'new_col'. df['new_col']= scalar (such as 5 or 6 in your case) also works and is equivalent to df['new_col']= [scalar]*len(df)

Dataframe

Cached

So a two-line code serves the purpose:

Hope this helps!