简体   繁体   中英

Symmetrical column values in pandas data frame

I have one set of variable as in below data frame:

    v1
----------
0   0.036286

1  -0.018490

2   0.011699

3   0.028955

4  -0.000373

Another set of variable in below data frame:

      v2
----------
41    12.31

42    12.20

43    12.12

44    12.31

45    12.47

1st columns are index columns. I want to add each row (v1+v2) to get v3. How do I make the index column values (0 to 4) and (41 to 45) symmetrical ( either 0-4) or (42-45) in both data fame?

I am working on pandas (python) jupyter notebook.

You have multiple options here I think:

  • You can directly concatenate them using pd.concat using the ignore_index=True parameter and then create the new column:
df = pd.concat(df1, df2, axis=1, ignore_index=True)
df['v3'] = df.sum(axis=1) # or df[['v1','v2']].sum(axis=1)
  • You can create a new vector/dataframe with the result:
v3 = df1['v1'].values + df2['v2'].values

which then you can manipulate as you prefer.

  • All other solutions people already proposed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM