I want to iterate through the rows in pandas and create a new column based on the value. I have my data set here:
Political Entity Recipient ID Recipient Recipient last name \
0 Candidates 4350 Whelan, Susan Whelan
1 Candidates 4350 Whelan, Susan Whelan
2 Candidates 4350 Whelan, Susan Whelan
3 Candidates 4350 Whelan, Susan Whelan
4 Candidates 15453 Mastroianni, Steve Mastroianni
Recipient first name Recipient middle initial Political Party of Recipient \
0 Susan NaN Liberal Party of Canada
1 Susan NaN Liberal Party of Canada
2 Susan NaN Liberal Party of Canada
3 Susan NaN Liberal Party of Canada
4 Steve NaN Liberal Party of Canada
Electoral District Electoral event Fiscal/Election date \
0 Essex 38th general election 2004-06-28
1 Essex 38th general election 2004-06-28
2 Essex 38th general election 2004-06-28
3 Essex 38th general election 2004-06-28
4 Windsor--Tecumseh 40th general election 2008-10-14
... Monetary amount Non-Monetary amount \
0 ... 800.0 0.0
1 ... 1280.0 0.0
2 ... 250.0 0.0
3 ... 1000.0 0.0
4 ... 800.0 0.0
I want to create a new column where it takes the political party and the year date and add the Monetary value. For example:
+------------------------------+----------------------------+--+--+--+
| 2004 Liberal Party of Canada | 2004 Green Party of Canada | | | |
+------------------------------+----------------------------+--+--+--+
| 8000 | 0 | | | |
+------------------------------+----------------------------+--+--+--+
| | | | | |
+------------------------------+----------------------------+--+--+--+
| | | | | |
+------------------------------+----------------------------+--+--+--+
I have created a couple of functions to help get started:
def year_political_column(row):
return row['Fiscal/Election date'][:4] + ' ' + row['Political Party of Recipient']
def monetary(row):
return row['Monetary amount']
Whenever I look up my solution it seems like you have to already have the column set. Can anyone lead me in the right direction?
Sample output should be:
Political Entity Recipient ID Recipient Recipient last name \
0 Candidates 4350 Whelan, Susan Whelan
1 Candidates 4350 Whelan, Susan Whelan
2 Candidates 4350 Whelan, Susan Whelan
3 Candidates 4350 Whelan, Susan Whelan
4 Candidates 15453 Mastroianni, Steve Mastroianni
Recipient first name Recipient middle initial Political Party of Recipient \
0 Susan NaN Liberal Party of Canada
1 Susan NaN Liberal Party of Canada
2 Susan NaN Liberal Party of Canada
3 Susan NaN Liberal Party of Canada
4 Steve NaN Liberal Party of Canada
Electoral District Electoral event Fiscal/Election date \
0 Essex 38th general election 2004-06-28
1 Essex 38th general election 2004-06-28
2 Essex 38th general election 2004-06-28
3 Essex 38th general election 2004-06-28
4 Windsor--Tecumseh 40th general election 2008-10-14
... Monetary amount Non-Monetary amount \
0 ... 800.0 0.0
1 ... 1280.0 0.0
2 ... 250.0 0.0
3 ... 1000.0 0.0
4 ... 800.0 0.0
Contribution given through Ontario first name Ontario last name \
0 NaN J M
1 NaN J
2 NaN B
3 NaN H
4 NaN H
Ontario Address Ontario city Ontario Province Ontario Postal Code \
0
Ontario Phone #
0
1
2
3
4
With all the political data I am looking for attached on the right.
crosstaborpivotdf.groupby('Political Party of Recipient')['Monetary amount'].sum(), and then use transpose: pandas.pydata.org/pandas-docs/stable/generated/…