How to get real and imaginary parts of a column in a Pandas dataframe?
Question:
import pandas as pd
I have a dataframe:
df=pd.DataFrame({'cmplxnumbers':[1+1j,2-2j,3*(1+1j)]})
I need to get the imaginary parts of the numbers in the column.
I do it by:
df.cmplxnumbers.apply(lambda number: number.imag)
I get as a result:
0 1.0
1 -2.0
2 3.0
Name: cmplxnumbers, dtype: float64
Which is as expected.
Is there any quicker, more straightforward method, perhaps not involving the lambda
function?
Answers:
Pandas DataFrame/Series builds on top of numpy array, so they can be passed to most numpy functions.
In this case, you can try the following, which should be faster than the non-vectorized .apply
:
df['imag'] = np.imag(df.cmplxnumbers)
df['real'] = np.real(df.cmplxnumbers)
Output:
cmplxnumbers imag real
0 1.000000+1.000000j 1.0 1.0
1 2.000000-2.000000j -2.0 2.0
2 3.000000+3.000000j 3.0 3.0
Or you can do agg
:
df[['real','imag']] = df.cmplxnumbers.agg([np.real, np.imag])
The .values
field of a Pandas object provides the underlying NumPy array, which has the real
and imag
fields. In summary:
real_part = pd_obj.values.real
imag_part = pd_obj.values.imag
import pandas as pd
I have a dataframe:
df=pd.DataFrame({'cmplxnumbers':[1+1j,2-2j,3*(1+1j)]})
I need to get the imaginary parts of the numbers in the column.
I do it by:
df.cmplxnumbers.apply(lambda number: number.imag)
I get as a result:
0 1.0
1 -2.0
2 3.0
Name: cmplxnumbers, dtype: float64
Which is as expected.
Is there any quicker, more straightforward method, perhaps not involving the lambda
function?
Pandas DataFrame/Series builds on top of numpy array, so they can be passed to most numpy functions.
In this case, you can try the following, which should be faster than the non-vectorized .apply
:
df['imag'] = np.imag(df.cmplxnumbers)
df['real'] = np.real(df.cmplxnumbers)
Output:
cmplxnumbers imag real
0 1.000000+1.000000j 1.0 1.0
1 2.000000-2.000000j -2.0 2.0
2 3.000000+3.000000j 3.0 3.0
Or you can do agg
:
df[['real','imag']] = df.cmplxnumbers.agg([np.real, np.imag])
The .values
field of a Pandas object provides the underlying NumPy array, which has the real
and imag
fields. In summary:
real_part = pd_obj.values.real
imag_part = pd_obj.values.imag