- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to calculate the absolute values in a pandas series with complex numbers?
Pandas series has a method for calculating absolute values of series elements. That function can also be used for calculating the absolute values of a series with complex numbers.
The abs() method in the pandas series will return a new series, which is having calculated absolute values of a series with complex numbers.
The absolute value of a complex number is $\sqrt{a^{2}+b^{2}}$ whereas a is the real value and b is the imaginary value of a complex number.
Example
# importing pandas packages import pandas as pd #creating a series with null data s_obj = pd.Series([2.5 + 3j, -1 - 3.5j, 9 - 6j, 3 + 2j, 10 + 4j, -4 + 1j]) print(s_obj, end='
') #calculate absolute values result = s_obj.abs() #print the result print(result)
Explanation
In the following example, we have created a series with complex numbers. After that, we have calculated absolute values for each complex number in series s_obj.
In our series, there are some positive complex numbers and negative complex numbers. We can see the absolute values for each complex in the output block below.
Output
0 2.500000+3.000000j 1 -1.000000-3.500000j 2 9.000000-6.000000j 3 3.000000+2.000000j 4 10.000000+4.000000j 5 -4.000000+1.000000j dtype: complex128 0 3.905125 1 3.640055 2 10.816654 3 3.605551 4 10.770330 5 4.123106 dtype: float64
In this output block, we have both series objects, one series with complex numbers and another with corresponding absolute values of complex numbered series. By using the abs() method of the pandas series we have calculated the absolute values of each complex number in the series.
Example
# importing pandas packages import pandas as pd #creating a series with null data s = pd.Series([-0.3 - 0.3j, 1.3 + 0.3j, 4.2 + 5.29j, -9.037-5.03j, 2.34+2.1j]) print(s, end='
') #calculate absolute values result = s.abs() #print the result print(result)
Explanation
This is another example of calculating the absolute value of a series with complex numbers. Initially, we have created a series object with complex numbers, then calculated the absolute values for those complex numbers.
Output
0 -0.300000-0.300000j 1 1.300000+0.300000j 2 4.200000+5.290000j 3 -9.037000-5.030000j 4 2.340000+2.100000j dtype: complex128 0 0.424264 1 1.334166 2 6.754561 3 10.342547 4 3.144137 dtype: float64
In the above output block we can see the a series object with resultant absolute values of complex numbered series.
To Continue Learning Please Login
Login with Google