When creating a graph with Python's Matplotlib, I tried to get the values from each data frame and create all the graphs for hundreds of lists, so I made a prototype method that can be done at once.
The method is as follows. ① Read the data frame ① ‘If the list is duplicated, put it together as a new data frame (remove duplicates) ② Create graphs in order using For Loop
This time, I used the kaggle data frame (OSIC Pulmonary Fibrosis Progression). By the way, the number of rows and columns is (1549,7), and the duplication is 176 rows. This time, we will create 176 graphs at once.
① Read the data frame
import pandas as pd
train_df = pd.read_csv("train.csv")
train_df
① ‘If the list is duplicated, put it together as a new data frame (remove duplicates)
new_df = train_df.groupby([train_df.Patient,train_df.Age,train_df.Sex, train_df.SmokingStatus])['Patient'].count() #Duplicate count
new_df.index = new_df.index.set_names(['id','Age','Sex','SmokingStatus'])
new_df = new_df.reset_index()
new_df.rename(columns = {'Patient': 'freq'},inplace = True)
new_df
② Create graphs in order using For Loop
import matplotlib.pyplot as plt
for num in new_df['id']: #Select the item you want to repeat with in(Select a data frame that eliminates duplication(new_df))
train2 = train_df.loc[train_df.Patient == num] #Select the column you want to get
graph = plt.plot(train2["Weeks"],train2["FVC"]) #Get the values for the X and Y axes of the graph
plt.xlabel("Weeks") #X-axis name
plt.ylabel("FVC") #Y-axis name
plt.title("{}".format(num)) #title
plt.show()
As shown in the figure, 176 graphs are displayed. I couldn't make it into a video, so I've included an image and omitted it.
Recommended Posts