
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Power of Elements in a Given Series using Python
Input − Assume, you have a series,
0 1 1 2 2 3 3 4
Output − And, the result for the power of all elements in a series is,
0 1 1 4 2 27 3 256
Solution 1
Define a Series.
Create transform method inside apply lambda power value. It is defined below, data.transform(lambda x:x**x)
data.transform(lambda x:x**x)
Solution 2
Define a Series.
Create an empty list
. Create for loop, iter all the items. Append elements to the list. It is defined below,
for i,j in data.items(): ls.append(m.pow(j,j))
Finally, convert the list into Series.
Example
Let us see the following implementation to get a better understanding.
import pandas as pd l = [1,2,3,4] data = pd.Series(l) print(data.transform(lambda x:x**x))
Output
0 1 1 4 2 27 3 256
Solution 3
Example
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)
Output
0 1.0 1 4.0 2 27.0 3 256.0
Advertisements