Python Pandas: Get the powers of an array values element-wise

Python Pandas: DataFrame Exercise-1 with Solution

Write a Pandas program to get the powers of an array values element-wise.

Sample data: {‘X’:[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}

Sample Solution :

Python Code :

import pandas as pd
df = pd.DataFrame({'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});

Sample Output:

    X   Y   Z                                                          
0  78  84  86                                                          
1  85  94  97                                                          
2  96  89  96                                                          
3  80  83  72                                                          
4  86  86  83                                       

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Python Pandas Data Series, DataFrame Exercises Home.
Next: Write a Pandas program to create and display a DataFrame from a specified dictionary data which has the index labels.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value


def tips_map_values(obj, fn):
  ret = {}
  for key in obj.keys():
    ret[key] = fn(obj[key])
  return ret
users = {
  'Owen': { 'user': 'Owen', 'age': 29 },
  'Eddie': { 'user': 'Eddie', 'age': 15 }

print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}


{'Owen': 29, 'Eddie': 15}