I have a data frame and I want to plot rows of a column in a loop
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
df = pd.DataFrame({
'name': ['joe', 'sue', 'mike'],
'x': ['0,1,5,3,4,5', '0,4,4,2,8', '0,4,6,7,8,9,0'],
'y': ['0,3,8', '1,9,5', '1,6,4,5,6,2,3,4,6']
})
print(df)
name x y
0 joe 0,1,5,3,4,5 0,3,8
1 sue 0,4,4,2,8 1,9,5
2 mike 0,4,6,7,8,9,0 1,6,4,5,6,2,3,4,6
I want to plot every rows of x for example i will plot 0,1,5,3,4,5
then 0,4,4,2,8
then 0,4,6,7,8,9,0
in a loop.
CodePudding user response:
You can use:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for row in df['x'].str.split(','): # explode string
data = np.array(row).astype(float) # convert str to float
ax.plot(data)
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_title('My figure')
plt.show()
Output: