0

I have a dataframe and a series. Both are having same index. I want to add series values as a row in dataframe along with the same index entries.

MY data frame and series are:

    enter code here
index                       test            num       %change

cos                       7% 20_DA      22          37.93
cos                       7% 5_MS       8           13.79
fla                       5% 60_DA      67          31.75
fla                       7% 20_DA      35          16.59
fla                       7% 5_MS       19          9.00
gol                       7% 20_DA      22          20.95
gol                       7% 5_MS       10          9.52
ill                       7% 20_DA      50          64.94
ill                       7% 5_MS       29          37.66
lag                       7% 20_DA      21          46.67
lag                       7% 5_MS       7           15.56
lag                       5% 60_DA      18          28.12
lag                       7% 20_DA      14          21.88
lag                       7% 5_MS       6           9.38
le                        5% 60_DA      66          24.81
le                        7% 20_DA      67          25.19
le                        7% 5_MS       34          12.78
li                        7% 20_DA      59          70.24
li                        7% 5_MS       23          27.38
po                        5% 60_DA      32          12.65
po                        7% 20_DA      38          15.02
po                        7% 5_MS       23          9.09

and series is

index       %change
cos         62.07
fla         68.25
gol         79.05
ill         35.06
lag         53.33
le          75.19
li          29.76
po          87.35

my output should look like

index                       test            num       %change

cos                       7% 20_DA         22           37.93
cos                       7% 5_MS          8            13.79
cos                         Nan             Nan         62.07

Please suggest a way. Let me know if title doesnt suggest the same intention what data does.

1 Answers1

2

Assuming your series is called series and your dataframe is called df, the solution would be:

pd.merge(df, pd.DataFrame(series), left_index=True, right_index=True)

Edit:

In looking a little more closely at your desired output, you may be looking for something more like:

new_df = pd.concat((data, series), axis=0)
new_df['%change'][len(df):] = new_df[new_df.columns.values[-1]][len(df):]
new_df.drop(new_df.columns.values[-1], axis=1, inplace=True)

This will give you the values of your series added as new rows to the '%change' column.

gaw89
  • 1,018
  • 9
  • 19