Bài 7: Hàm cơ bản – Series – Python Panda

Trang chủ » Training » Bài 7: Hàm cơ bản – Series – Python Panda
22/02/2022 Training 152 viewed
Chúng ta đã tìm hiểu về ba Cấu trúc Dữ liệu của Panda và cách tạo chúng. Tập trung vào các đối tượng DataFrame vì tầm quan trọng của nó trong việc xử lý dữ liệu thời gian thực và cũng thảo luận về một vài DataStructures khác.

1. Các hàm cơ bản trong series :

Ví dụ1 :
import pandas as pd
import numpy as np

#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print s
Kết quả :
0   0.967853
1  -0.148368
2  -1.395906
3  -1.758394
dtype: float64
Ví dụ 2 :
import pandas as pd
import numpy as np

#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("The axes are:")
print s.axes
Kết quả :
The axes are:
[RangeIndex(start=0, stop=4, step=1)]
Kết quả trên là một định dạng nhỏ gọn của series các giá trị từ 0 đến 5, tức là [0,1,2,3,4].
Ví dụ 3 : Trả về giá trị Boolean cho biết Đối tượng có rỗng hay không. True chỉ ra rằng đối tượng trống.
import pandas as pd
import numpy as np

#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("Is the Object empty?")
print s.empty
Kết quả :
Is the Object empty?
False
Ví dụ 4 : Trả về số kích thước của đối tượng. Theo định nghĩa, series là cấu trúc dữ liệu 1D(1 chiều)
import pandas as pd
import numpy as np

#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s

print ("The dimensions of the object:")
print s.ndim
Kết quả :
import pandas as pd
import numpy as np

#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s

print ("The dimensions of the object:")
print s.ndim
Ví dụ 5 : Trả về độ dài của Series
import pandas as pd
import numpy as np

#Create a series with 4 random numbers
s = pd.Series(np.random.randn(2))
print s
print ("The size of the object:")
print s.size
Kết quả :
0   3.078058
1  -1.207803
dtype: float64

The size of the object:
2
Ví dụ 6 :Trả về dữ liệu thực tế trong series dưới dạng mảng.
import pandas as pd
import numpy as np

#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s

print ("The actual data series is:")
print s.values
Kết quả :
0   1.787373
1  -0.605159
2   0.180477
3  -0.140922
dtype: float64

The actual data series is:
[ 1.78737302 -0.60515881 0.18047664 -0.1409218 ]
Ví dụ 7 : Head & Tail
Để xem một mẫu nhỏ của một Series hoặc DataFrame, sử dụng phương thức head () và tail ().
head () trả về n hàng đầu tiên (quan sát các giá trị chỉ số).Số phần tử mặc định để hiển thị là năm
mport pandas as pd
import numpy as np

#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print ("The original series is:")
print s

print ("The first two rows of the data series:")
print s.head(2)
Kết quả :
The original series is:
0   0.720876
1  -0.765898
2   0.479221
3  -0.139547
dtype: float64

The first two rows of the data series:
0   0.720876
1  -0.765898
dtype: float64
tail () trả về n hàng cuối cùng (quan sát các giá trị chỉ số). Số phần tử mặc định để hiển thị là năm
import pandas as pd
import numpy as np

#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print ("The original series is:")
print s

print ("The last two rows of the data series:")
print s.tail(2)
Kết quả :
The original series is:
0 -0.655091
1 -0.881407
2 -0.608592
3 -2.341413
dtype: float64

The last two rows of the data series:
2 -0.608592
3 -2.341413
dtype: float64

2. Các thuộc tính, phương thức của tham số trong series

  1. axes : Trả về danh sách các trục hàng
  2. dtype : Trả về object
  3. empty :Trả về True nếu series rỗng
  4. ndim : Trả về số chiều,mặc định là 1.
  5. size : Trả về số phần tử trong dữ liệu
  6. values : Trả về series như ndarray
  7. head() : Trả về n dòng đầu
  8. tail() : Trả về n dòng cuối
Chia sẻ:
Tags:
TOP HOME