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

เขียนโปรแกรม Python แทนที่ตำแหน่งดัชนีคี่ใน Series ด้วยสระพิมพ์ใหญ่สุ่ม


อินพุต - สมมติคุณมี Series

0    1 1    2 2    3 3    4 4    5

เอาต์พุต -

ผลลัพธ์หลังแทนที่ดัชนีคี่ด้วยสระพิมพ์ใหญ่

0    1 1    A 2    3 3    U 4    5

วิธีแก้ปัญหา

  • สร้าง Series

  • กำหนดสระพิมพ์ใหญ่

  • วนลูปถ้าดัชนีคี่ ให้สุ่มสระพิมพ์ใหญ่แทนที่

ตัวอย่าง

import pandas as pd
import random as r
l = [1,2,3,4,5]
data = pd.Series(l)
print("Given series:\n", data)
vowels = list("AEIOU")
for i,j in data.items():
    if(i%2!=0):
        data[i]="".join(r.choice(vowels))
print("modified series:-\n",data)

ผลลัพธ์

Given series: 0    1 1    2 2    3 3    4 4    5 dtype: int64 modified series:- 0    1 1    O 2    3 3    E 4    5 dtype: object