in Education by
Say I have a dictionary that the key of year, and the corresponding value of a list of values. Is there a way to apply that value across the row of a dataframe. For instance if my key is "1995" how do I apply element 1 of the dictionary to column 1, element 2 to column 2 and etc for the whole entire dictionary? my dictionary is as follows: my_dict = {'1995' : [.99,.98,.96],'1994' : [.9995,.986,.97]} now I have a dataframe organized as such df = pd.DataFrame({'issue year' : {1995,1994} 'legal entitiy': {'a',"a"} 'x': {100,98}, 'y': {98,97}, 'z':{95,80}) I wish to apply across each row such that for 1995 we hit 100 with .99,98 with .98 anda 95 with .96 1994 would get 98 with .995, 97 with .986 and so on. If there was a 3rd year column x y and z would have one more entry and each would get hit with a factor for that year. pd.DataFrame({'issue year' : {1995,1994} 'legal entitiy': {'a',"a"} 'x': {100*.99, 98 * .9995}, 'y': {98*.98, 97*.986}, 'z':{95*.96, 80 * .97}) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
First you transform your dictionary into a Dataframe with the names of the columns you want to apply your operation on e.g. df_1 = pd.DataFrame.from_dict(my_dict, orient='index',columns=['x','y','z']) df_1.index = df_1.index.astype(int) Then you can use slices to select the part of dataframe you want to perform the operation on and the use a standard arithmetic operator. df = df.set_index('issue year') df.loc[df_1.index,df_1.columns] = df.loc[df_1.index,df_1.columns]*df_1 df = df.reset_index() Which will result in the desired dataframe

Related questions

0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Currently I'm returning column name of the max value in the each row. df['Active'] = df.idxmax( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have a simple method to search a pandas dataframe column for a list of keywords; however, I'd like to create a ... do everyth 28,passei o dia com o meu amor comemo demai...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I have a dataset with employee payroll information (df2). It has a date, job title, shift start ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    18F-AV-1451-A07 Value refer to another sheet called "CONTENT" in which column "B" and row "3". ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a csv file and I am reading this file in python using pandas. I want to read each row of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am trying to create a pandas dataframe that combines all children into one row class Parent(Base): ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a pandas series containing a list of dictionaries. I'd like to parse the contents of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I'm trying to get the number of rows of dataframe df with Pandas, and here is my code. Method 1: total_rows ... What am I doing wrong? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...