I have this function written in python. I want this thing show only one value.
Here's the code
def show_data():
df = pd.DataFrame(myresult, columns=['Year', 'Production (Ton)'])
df['Max Prod'] = df['Production (Ton)'].max())
print(df)
And of course the output is this
Year Production (Ton) Max Prod
0 2010 339491 366999
1 2011 366999 366999
2 2012 361986 366999
3 2013 329461 366999
4 2014 355464 366999
5 2015 344998 366999
6 2016 274317 366999
7 2017 200916 366999
8 2018 217246 366999
9 2019 119830 366999
10 2020 66640 366999
Since it has the same value, I want the output like this
Year Production (Ton) Max Prod
0 2010 339491 366999
1 2011 366999
2 2012 361986
3 2013 329461
4 2014 355464
5 2015 344998
6 2016 274317
7 2017 200916
8 2018 217246
9 2019 119830
10 2020 66640
What should I change or add to my code?
CodePudding user response:
You can use shift
to generate a mask that can be used to replace duplicate consecutive values:
df.loc[df['Max Prod'] == df['Max Prod'].shift(1), 'Max Prod'] = ''
Output:
>>> df
Year Production (Ton) Max Prod
0 2010 339491 366999
1 2011 366999
2 2012 361986
3 2013 329461
4 2014 355464
5 2015 344998
6 2016 274317
7 2017 200916
8 2018 217246
9 2019 119830
10 2020 66640
CodePudding user response:
You could also have the function as:
def show_data():
df = pd.DataFrame(myresult, columns=['Year', 'Production (Ton)'])
df['Max Prod'] = ''
df.iloc[0, -1] = df['Production (Ton)'].max()
print(df)
CodePudding user response:
Given what you have now:
def show_data():
df = pd.DataFrame(myresult, columns=['Year', 'Production (Ton)'])
df['Max Prod'] = df['Production (Ton)'].max())
df = df['Max Prod'].drop_duplicates()
df = df.fillna('')
print(df)
Output:
Year Production-(Ton) Max Prod
0 2010 339491 549713
1 2011 366999
2 2012 361986
3 2013 329461
4 2014 355464
5 2015 344998
6 2016 274317
7 2017 200916
8 2018 217246
9 2019 119830
10 2020 66640