Categories
Python Answers

How to find row where values for column is maximal in a Python Pandas DataFrame

Sometimes, we want to find row where values for column is maximal in a Python Pandas DataFrame.

In this article, we’ll look at how to find row where values for column is maximal in a Python Pandas DataFrame.

How to find row where values for column is maximal in a Python Pandas DataFrame

To find row where values for column is maximal in a Python Pandas DataFrame, we can use the idxmax method.

For instance, we write

import pandas
import numpy as np

df = pandas.DataFrame(np.random.randn(5,3),columns=['A','B','C'])
df['A'].idxmax()

to create the df data frame.

And then we use

df['A'].idxmax()

to get the index of the highest value in columns A.

Conclusion

To find row where values for column is maximal in a Python Pandas DataFrame, we can use the idxmax method.

Categories
Python Answers

How to add a line plot display date on x-axis with a Python Pandas Dataframe?

Sometimes, we want to add a line plot display date on x-axis with a Python Pandas Dataframe, we can use matplotlib.

In this article, we’ll look at how to add a line plot display date on x-axis with a Python Pandas Dataframe.

How to add a line plot display date on x-axis with a Python Pandas Dataframe?

To add a line plot display date on x-axis with a Python Pandas Dataframe, we can use matplotlib.

For instance, we write

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

df = pd.DataFrame({'date':['20170527','20170526','20170525'],'ratio1':[1,0.98,0.97]})
df['date'] = pd.to_datetime(df['date'])

usePandas=True
#Either use pandas
if usePandas:
    df = df.set_index('date')
    df.plot(x_compat=True)
    plt.gca().xaxis.set_major_locator(dates.DayLocator())
    plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%d\n\n%a'))
    plt.gca().invert_xaxis()
    plt.gcf().autofmt_xdate(rotation=0, ha="center")
# or use matplotlib
else:
    plt.plot(df["date"], df["ratio1"])
    plt.gca().xaxis.set_major_locator(dates.DayLocator())
    plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%d\n\n%a'))
    plt.gca().invert_xaxis()

plt.show()

to call plt.gca to create the axes.

And then we call invert_xaxis to invert the x-axis.

Then we create the figure with gcf.

Finally, we call plt.show to show the plot.

Conclusion

To add a line plot display date on x-axis with a Python Pandas Dataframe, we can use matplotlib.

Categories
Python Answers

How to pivot a dataframe in Python Pandas?

To pivot a dataframe in Python Pandas, we call the pivot_table method.

For instance, we write

pd.pivot_table(df, values = 'Value', index=['Country','Year'], columns = 'Indicator').reset_index()

to call pivot_table with the df data frame.

We set the values argument to the olumn with the values.

index has the index columns.

columns has other columns.

And we call reset_index to reassign the indices for each row.

Categories
Python Answers

How to convert a Python Pandas DataFrame to a list of lists?

Sometimes, we want to convert a Python Pandas DataFrame to a list of lists.

In this article, we’ll look at how to convert a Python Pandas DataFrame to a list of lists.

How to convert a Python Pandas DataFrame to a list of lists?

To convert a Python Pandas DataFrame to a list of lists, we can use the tolist method.

For instance, we write

df = pd.DataFrame([[1,2,3],[3,4,5]])
lol = df.values.tolist()

to create a data frame df.

Then we convert the df data frame a to a list of lists with

df.values.tolist()

and assigned the returned nested list to lol

Conclusion

To convert a Python Pandas DataFrame to a list of lists, we can use the tolist method.

Categories
Python Answers

How to insert list into a cell with Python Pandas?

Sometimes, we want to insert list into a cell with Python Pandas.

In this article, we’ll look at how o insert list into a cell with Python Pandas.

How to insert list into a cell with Python Pandas?

To insert list into a cell with Python Pandas, we use at to get the row and column we want to insert the list into.

Then we assign the list to it.

For instance, we write

df['B'] = df['B'].astype('object')
df.at[1, 'B'] = [1, 2, 3]

to convert column B to an object with astype.

And then we assign the value at row index 1 and column B to a list with

df.at[1, 'B'] = [1, 2, 3]

Conclusion

To insert list into a cell with Python Pandas, we use at to get the row and column we want to insert the list into.