1.基本:
import pandas as pd
data = pd.Series(["你好","今天天氣真好","我卻只能宅在家","嗚嗚嗚","So sad"])
print(data)
執行結果:
0 你好
1 今天天氣真好
2 我卻只能宅在家
3 嗚嗚嗚
4 So sad
2.顯示長度:
import pandas as pd
data = pd.Series(["你好","今天天氣真好","我卻只能宅在家","嗚嗚嗚","So sad"])
print(data.str.len())
執行結果:
0 2
1 6
2 7
3 3
4 6
dtype: int64
3.控制大小寫:
import pandas as pd
data = pd.Series(["你好","今天天氣真好","我卻只能宅在家","嗚嗚嗚","So sad"])
print(data.str.upper())
print(data.str.lower())
執行結果:
0 你好
1 今天天氣真好
2 我卻只能宅在家
3 嗚嗚嗚
4 SO SAD
dtype: object
0 你好
1 今天天氣真好
2 我卻只能宅在家
3 嗚嗚嗚
4 so sad
dtype: object
4.串接:
import pandas as pd
data = pd.Series(["你好","今天天氣真好","我卻只能宅在家","嗚嗚嗚","So sad"])
print(data.str.cat(sep=",")) #sep可以自訂
5.判斷是否包含指定字元:
import pandas as pd
data = pd.Series(["你好","今天天氣真好","我卻只能宅在家","嗚嗚嗚","So sad"])
print(data.str.contains("真好"))
執行結果:
0 False
1 True
2 False
3 False
4 False
dtype: bool
6.取代指定內容:
import pandas as pd
data = pd.Series(["你好","今天天氣真好","我卻只能宅在家","嗚嗚嗚","So sad"])
print(data.str.replace("宅在家","打鐵人"))
執行結果:
0 你好
1 今天天氣真好
2 我卻只能打鐵人
3 嗚嗚嗚
4 So sad