Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การหาความยาวของคำใน Pandas Series ที่กำหนด


ในงานนี้ เราจะหาความยาวของสตริงใน Pandas Series เราจะใช้ฟังก์ชัน str.len() ในไลบรารี Pandas สำหรับวัตถุประสงค์นี้

อัลกอริทึม

ขั้นตอนที่ 1: กำหนด Pandas Series ของสตริง
ขั้นตอนที่ 2: หาความยาวของแต่ละสตริงด้วยฟังก์ชัน str.len()
ขั้นตอนที่ 3: แสดงผลลัพธ์

ตัวอย่างโค้ด

import pandas as pd
series = pd.Series(["Foo", "bar", "London", "Quarantine"])
print("Series: \n", series)
length = series.str.len()
print("Length:\n", length)

ผลลัพธ์

Series:
0     Foo
1     bar
2     London
3     Quarantine
dtype: object
Length:
0     3
1     3
2     6
3     10
dtype: int64