อินพุต - สมมติคุณมี series
0 1 1 2 2 3 3 4
เอาต์พุต - ผลลัพธ์กำลังของทุกสมาชิก
0 1 1 4 2 27 3 256
วิธีที่ 1
สร้าง Series
ใช้ transform กับ lambda x:x**x
data.transform(lambda x:x**x)
วิธีที่ 2
สร้าง Series
สร้างลิสต์ว่าง วนลูปคำนวณกำลังด้วย math.pow เก็บในลิสต์
แปลงลิสต์เป็น Series
ตัวอย่าง
import pandas as pd l = [1,2,3,4] data = pd.Series(l) print(data.transform(lambda x:x**x))
ผลลัพธ์
0 1 1 4 2 27 3 256
วิธีที่ 3
ตัวอย่าง
import pandas as pd
import math as m
l = [1,2,3,4]
data = pd.Series(l)
ls = []
for i,j in data.items():
ls.append(m.pow(j,j))
result = pd.Series(ls)
print(result)
ผลลัพธ์
0 1.0 1 4.0 2 27.0 3 256.0