Question

Write a python function that will taken in a df (dataframe) and return a dictionary with...

Write a python function that will taken in a df (dataframe) and return a dictionary with the corresponding data types. You may need to instantiate an empty dictionary in your function. The keys will be the column headers of the df and the values will be the data types.

Homework Answers

Answer #1

Code:

import pandas as pd
sample_dataframe = pd.DataFrame()
sample_dataframe['Name'] = ['Mike','Aston']
sample_dataframe['Age'] = [24,22]
sample_dataframe['Address'] = ['CA','NY']

def datatype_dict(dataframe):
    # get the columns name of the dataframe
    column = list(dataframe.columns)

    # get the datatype of each items of each columns
    datatypes = [type(dataframe[col][0]) for col in column]   # using list comprehension

    # creating a dictionary now
    dict_datatype = {}
    for loop1 in range(len(column)):
        dict_datatype[column[loop1]] = datatypes[loop1]
        
    return(dict_datatype)

# calling the function
datatype_dict(sample_dataframe)

Result:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program to create a dictionary from two lists without losing duplicate values. If...
Write a Python program to create a dictionary from two lists without losing duplicate values. If there is more values in the key list, then provided key should return an empty set if there is no match. (Hint: use defaultdict) Example: class_list = ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII','Class-IX'] id_list = [1, 2, 2, 3] Output: assuming you store the values in a data structure named temp print(temp["Class-V"]) # {1} print(temp["Class-IX"]) # set() can you please try and do on google colab
Write a Python function called sumNxN with three parameters. The first parameter is a nested list...
Write a Python function called sumNxN with three parameters. The first parameter is a nested list (matrix) representing a matrix of size N x N. The second parameter is the integer N, and the third is a list of N zeros. Modify the list of zeros so that each entry is the sum of the corresponding column. There is no return. Note: You may assume the sizes provided are all correct.
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
PYTHON: Write a function called keepShouting. This function does not need any input parameters. The function...
PYTHON: Write a function called keepShouting. This function does not need any input parameters. The function should simply repeat whatever the user enters but in all capitals! The function should ask the user to enter a phrase. The phrase then should be capitalized and printed to the screen. If the user just hits return (which will produce the empty string) then the program should stop. Hint: Use a while loop and string methods # keepShouting() """ The function call should...
In python def reverse_lookup(input_dict, test_value): # Complete this function to find all the keys in a...
In python def reverse_lookup(input_dict, test_value): # Complete this function to find all the keys in a dictionary that map to the input value. # input1: input_dict (dict) # input2: test_value # output: list of keys # YOUR CODE HERE It has to pass these tests input_dict = { 'January': 31, 'February': 28, 'March': 31, 'April': 30, 'May': 31, 'June': 30, 'July': 31, 'August': 31, 'September': 30, 'October': 31, 'November': 30, 'December': 31, } assert reverse_lookup(input_dict, 31) == ['January', 'March', 'May',...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Python Question: Write a function which returns the sum of squares of the integers 1 to...
Python Question: Write a function which returns the sum of squares of the integers 1 to n. For example, the sum of the squares from 1 to 4 is 1 + 4 + 9 + 16, or 30. You may choose what should be returned if n == 0 You may not use the built-in Python sum function. The Solution Must be recursive >>> sum_of_squares(1) 1 >>> sum_of_squares(4) 30 >>> sum_of_squares(8) # 64 + 49 + 36 + ... +...
solve in python 3.8 please The question is about Strings Modify your function so that it...
solve in python 3.8 please The question is about Strings Modify your function so that it can handle the following two situations: The string given as an argument has no characters. In this case, you should return the empty string. The string given as an argument has one character. In this case, you should return the one-character string given to you. Without any consideration for these cases, your function probably causes an error or returns the wrong result in the...