Python Forum
Python for Begginers (DataFrame, Dictionary)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for Begginers (DataFrame, Dictionary)
#1
update expense table as follows:
* everyone paid for accomodation 100
* everyone paid for food 50
* everyone paid for transportation 80, except Diego,
who went to visit family and paid a 100 more
* everyone paid 60 for fun
* Option 1: use a dictionary to enter all expense values
then call update_expenses function from Q1
* Option 2: update expense table directly using DataFrame.loc["row_name","colmn_name"]
"""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Q.2. update new values in expense_table dataframe:
* create function named 'update_expenses'
* use a dataframe as function input
* insert a dictionary with new values as function input
* update new values in expenses_table
using a loop over indexes and .loc indexing
* return updated expense_table
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""

Getting infinite number of errors when trying to update a DataFrame by the Dictionary values.

relevant code of mine:

*def update_expenses (df,d):

for row in df:

???

df.loc['Diego']['Fun'] = 180


return df*


Functions' input should be a DataFrame and a Dictionary
and to return the same DataFrame when all rows replaced by values from dictionary (same values).

Example of what I mean,
Here is the DataFrame: (*Sorry it gets messy when posted)

Accomodation Food Transportation Fun
Dana 0.0 0.0 0.0 0.0
Avital 0.0 0.0 0.0 0.0
Yossi 0.0 0.0 0.0 0.0
Diego 0.0 0.0 0.0 0.0

And the Dictinary:

d = {'Accomodation': 100, 'Food': 50, 'Transportation': 80, 'Fun': 60}

Result should be:

Accomodation Food Transportation Fun
Dana 100 50 80 100
Avital 100 50 80 100
Yossi 100 50 80 100
Diego 100 50 80 **180**



Went through thosands of explanations but get the right way to make it.


Sincerely apologize for such silly question,
Thanks in advance.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020