Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Combined with NumPy

Pandas Combined with NumPy

Pandas is built on NumPy and the two are tightly integrated. Understanding how they interact enables more efficient data processing and scientific computing.


Converting between them

Converting between DataFrame/Series and NumPy

Example

import

pandas

as

pd

import

numpy

as

np

Convert a DataFrame to a NumPy array

df

=

pd.

DataFrame

(

{

   

"A"

:

[

1

,

2

,

3

]

,

   

"B"

:

[

4

,

5

,

6

]

}

)

arr

=

df.

to_numpy

(

)

print

(

"DataFrame converted to array:"

)

print

(

arr

)

print

(

f

"Type: {type(arr)}"

)

print

(

)

Convert a Series to an array

s

=

pd.

Series

(

[

1

,

2

,

3

]

)

arr

=

s.

values

 

or s.to_numpy()

print

(

"Series converted to array:"

)

print

(

arr

)

print

(

)

Convert NumPy to DataFrame

arr

=

np.

array

(

[

[

1

,

2

]

,

[

3

,

4

]

,

[

5

,

6

]

]

)

df

=

pd.

DataFrame

(

arr

,

columns

=

[

"A"

,

"B"

]

)

print

(

"Array converted to DataFrame:"

)

print

(

df

)


Using NumPy functions in Pandas

Example

import

pandas

as

pd

import

numpy

as

np

s

=

pd.

Series

(

[

1

,

2

,

3

,

4

,

5

]

)

Use NumPy functions

print

(

"Absolute value:"

,

np.

abs

(

s

)

)

print

(

"Square root:"

,

np.

sqrt

(

s

)

)

print

(

"Exponential:"

,

np.

exp

(

s

)

)

print

(

"Logarithm:"

,

np.

log

(

s

)

)

print

(

)

Conditional filtering

print

(

"Values greater than 3:"

)

print

(

s

[

s

3

]

)

Vectorized operations

Example

import

pandas

as

pd

import

numpy

as

np

df

=

pd.

DataFrame

(

{

"A"

:

[

1

,

2

,

3

]

,

"B"

:

[

4

,

5

,

6

]

}

)

Vectorized operations

print

(

"A + B:"

,

(

df

[

"A"

]

[

"B"

]

)

.

tolist

(

)

)

print

(

"A * B:"

,

(

df

[

"A"

]

[

"B"

]

)

.

tolist

(

)

)

print

(

"A > B:"

,

(

df

[

"A"

]

df

[

"B"

]

)

.

tolist

(

)

)

print

(

)

Using apply for element-wise computation

result

=

df.

apply

(

lambda

x: np.

multiply

(

x

,

2

)

)

print

(

"Each element * 2:"

)

print

(

result

)


Numerical computation techniques

Example

import

pandas

as

pd

import

numpy

as

np

df

=

pd.

DataFrame

(

{

   

"A"

:

[

1

,

2

,

3

,

4

,

5

]

,

   

"B"

:

[

10

,

20

,

30

,

40

,

50

]

}

)

Calculate the percentage change of A relative to B

df

[

"Change Rate"

]

=

np.

divide

(

df

[

"A"

]

,

df

[

"B"

]

,

where

=

df

[

"B"

]

!=

0

)

100

print

(

"Percentage change:"

)

print

(

df

)

print

(

)

Using np.where for conditional assignment

df

[

"Label"

]

=

np.

where

(

df

[

"A"

]

3

,

"Large"

,

"Small"

)

print

(

"Conditional labels:"

)

print

(

df

)


Statistical computation

Example

import

pandas

as

pd

import

numpy

as

np

s

=

pd.

Series

(

[

1

,

2

,

3

,

4

,

5

,

6

,

7

,

8

,

9

,

10

]

)

NumPy statistical functions

print

(

f

"Mean: {np.mean(s):.2f}"

)

print

(

f

"Standard deviation: {np.std(s):.2f}"

)

print

(

f

"Maximum: {np.max(s)}"

)

print

(

f

"Minimum: {np.min(s)}"

)

print

(

)

Median

print

(

f

"Median: {np.median(s)}"

)

The deep integration of Pandas and NumPy makes complex data processing simple and efficient.