1

I have a dataframe with column A and B. I want to create a third column which is square root of the sum of squares of two columns.

Below is my sample data:

A   B
10  7
10  8
9   8
10  11
13  5
3   0
12  8
12  9
11  10
10  11

Below is the code that I have tried:

df['vib_mot_sqrt'] = df[(np.sqrt(df['X']**2) + (df['index']**2))]

KeyError: "None of [Float64Index([6.0, 6.0, 11.0, 15.0, 16.0, 33.0, 42.0, 55.0, 73.0, 87.0], dtype='float64')] are in the [columns]"

AB14
  • 397
  • 2
  • 13
  • Sum of square root? Where are you doing that? I just see one `sqrt` – Sheldore May 14 '19 at 12:18
  • yes, both would you've said and your code contradict themselves – yatu May 14 '19 at 12:19
  • Your question title and the code implementation are confusing. Please explain what do you want people to solve – Sheldore May 14 '19 at 12:21
  • @ Sheldore thanks for point out. It was a typing error. I have edited the title and summary part of my question. Hope this is clear. – AB14 May 14 '19 at 12:28

3 Answers3

1

Use square root with both columns and sum:

df['vib_mot_sqrt'] = df['A']**.5 + df['B']**.5
print (df)
    A   B  vib_mot_sqrt
0  10   7      5.808029
1  10   8      5.990705
2   9   8      5.828427
3  10  11      6.478902
4  13   5      5.841619
5   3   0      1.732051
6  12   8      6.292529
7  12   9      6.464102
8  11  10      6.478902
9  10  11      6.478902

My original answer:

df['vib_mot_sqrt'] = np.sqrt(df['A']**2 + (df['B']**2))
print (df)
    A   B  vib_mot_sqrt
0  10   7     12.206556
1  10   8     12.806248
2   9   8     12.041595
3  10  11     14.866069
4  13   5     13.928388
5   3   0      3.000000
6  12   8     14.422205
7  12   9     15.000000
8  11  10     14.866069
9  10  11     14.866069
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @Sheldore - Thank you, answer was changed. – jezrael May 14 '19 at 12:23
  • 1
    @ jezrael, Great ! thanks for the input. I was using df over the np.sqrt earlier. There was no need for that. Now it's absolutely fine. – AB14 May 14 '19 at 19:10
1

Could you please clarify do you want to calculate exactly one value or value for each row?

If you want to calculate value for each row and add it to the new column this should do it:

df['C']= np.sqrt(df['A']**2+df['B']**2)

Output:

    A   B   C
0   4   5   6.403124
1   3   3   4.242641
2   1   1   1.414214
JureZu
  • 26
  • 4
  • 1
    I wanted to calculate the value for each row. You have rightly answered my query. Thanks a lot! – AB14 May 14 '19 at 19:28
  • I get a “runtimewarning: invaid value encountered in sqrt.” Any tips for solving this? – Catlover Mar 08 '23 at 18:02
0

Use np.sqrt(x) for square root and x**a for the power:

df = pd.DataFrame([
                   [10 , 7],
                   [10 , 8],
                   [9  , 8],
                   [10 , 11],
                   [13 , 5],
                   [3  , 0],
                   [12 , 8],
                   [12 , 9],
                   [11 , 10],
                   [10 ,11],],
                  columns=["A"  , "B"])

df["sqrt(A²+B²)"] = np.sqrt(df["A"]**2 + df["B"]**2)

print(df)
#     A   B  sqrt(A²+B²)
# 0  10   7    12.206556
# 1  10   8    12.806248
# 2   9   8    12.041595
# 3  10  11    14.866069
# 4  13   5    13.928388
# 5   3   0     3.000000
# 6  12   8    14.422205
# 7  12   9    15.000000
# 8  11  10    14.866069
# 9  10  11    14.866069
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40