I'm pretty new to Python. Im trying to define a function for the below set of data. Sample data
I'm first looking for the max value in cell 3
. Based on this max value I'm checking which column given in the last 8 columns of the data has the highest value against it.
For example, As per the given data, Max value in cell 3 is 1470758
. Now I'm checking which columns from column cell 9
to cell 16
has the highest value against this max value. In the case of this sample data the answer should be cell 10 with a value of 7201. So the output should be cell 10.
Here's my code:
def winner(filename):
data=pd.read_csv(filename, sep=',')
maxC=data.npop.max()
while data.loc[data['npop']]==maxC:
data3=data.iloc[:,-8:].max()
#missing code
winner("demo.csv")
Please help. I didn't understand what I should be writing in the missing code section.
Line by line explanation of code is given with comments.
Try this :
def winner(filename):
df=pd.read_csv(filename, sep=',') # Read the csv into dataframe.
column_names = list(df.columns.values) # Get list of column names
max_col3_index = df['col3'].idmax() # this will return the index of max value in `col3` column.
row_data = df.loc[max_col3_index, column_names[-8:]] # get series of data present in last 8 columns at above index.
final_column_name = row_data.idxmax() # Get the name of column having max value in above series.
print(final_column_name)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.