Home > Développement > Python > Numpy & Pandas

Numpy & Pandas

import numpy as np
import pandas as pd
labels = ['a', 'b', 'c']
my_list = [10, 20, 30]
arr = np.array([10, 20, 30])
d = {'a': 10, 'b': 20, 'c': 30}
pd.Series(data=my_list)
0 10
1 20
2 30
dtype: int64
pd.Series(data = my_list, index = labels)
a 10
b 20
c 30
dtype: int64
pd.Series(my_list, labels)
a 10
b 20
c 30
dtype: int64
pd.Series(labels)
0 a
1 b
2 c
dtype: object
ser1 = pd.Series([1,2,3,4], index = ['France', 'Belgiqe', 'Angleterre', 'Espagne'])
ser1
France 1
Belgique 2
Angleterre 3
Espagne 4
dtype: int64
ser2 = pd.Series([1,2,3,4], index = ['France', 'Belgiqe', 'Angleterre', 'Espagne'])
ser2
France 1
Belgique 2
Angleterre 3
Espagne 4
dtype: int64
ser1 + ser2
France 2
Belgique 4
Angleterre 6
Espagne 8
dtype: int64
ser3 = pd.Series([1,2,3,4,5], index = ['France', 'Belgiqe', 'Angleterre', 'Espagne', 'Russie'])
ser1 + ser3
Angleterre 6.0
Belgique 4.0
Espagne 8.0
France 2.0
Russie NaN
dtype: float64
from numpy.random import randn
np.random.seed(101)
df = pd.DataFrame(randn(5,4), index='A B C D E'.split(), columns='W X Y Z'.split())
df
W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 0.651118 -0.319318 -0.848077 0.605965
C -2.018168 0.740122 0.528813 -0.589001
D 0.188695 -0.758872 -0.933237 0.955057
E 0.190794 1.978757 2.605967 0.683509
df(['W'])
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-35-3b8376c868ae> in <module>
----> 1 df(['W'])

TypeError: 'DataFrame' object is not callable
df.W
A 2.706850
B 0.651118
C -2.018168
D 0.188695
E 0.190794
Name: W, dtype: float64
panda.core.series.Series
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-38-57b3def43b2f> in <module>
----> 1 panda.core.series.Series

NameError: name 'panda' is not defined
pd.core.series.Series
pandas.core.series.Series
df.drop('E', axis = 0)
W X Y Z
A 2.706850 0.628133 0.907969 0.503826
B 0.651118 -0.319318 -0.848077 0.605965
C -2.018168 0.740122 0.528813 -0.589001
D 0.188695 -0.758872 -0.933237 0.955057
df.drop('E')
df.loc['B', 'X']
-0.31931804459303326
df[df['W'] > 0][['Y', 'X']]
Y X
A 0.907969 0.628133
B -0.848077 -0.319318
D -0.933237 -0.758872
E 2.605967 1.978757
df.reset_index()
index W X Y Z
0 A 2.706850 0.628133 0.907969 0.503826
1 B 0.651118 -0.319318 -0.848077 0.605965
2 C -2.018168 0.740122 0.528813 -0.589001
3 D 0.188695 -0.758872 -0.933237 0.955057
4 E 0.190794 1.978757 2.605967 0.683509