2

I have the following code:

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6, 6), index=dates, columns=["a","b","c","a_x","b_x","c_x"])

which results in the following:

                a           b            c         a_x         b_x         c_x
2013-01-01  -0.871681   0.938965    -0.804039   0.329384    -1.211573   0.160477
2013-01-02  1.673895    2.017654    2.181771    0.336220    0.389709    0.246264
2013-01-03  -0.670211   -0.561792   -0.747824   -0.837123   0.129040    1.044153
2013-01-04  -0.571023   -0.430249   0.024393    1.017622    1.072909    0.816249
2013-01-05  0.074952    -0.119953   0.245248    2.658196    -1.525059   1.131054
2013-01-06  0.203816    0.379939    -0.162919   -0.674444   -0.650636   0.415143

I want to generate simple line plot charts - a total of three, each plotting the couples:

a and a_x, b and b_x and c and c_x

I know how to generate charts but since the table is big and has the same pattern in the column naming conventions I was thinking if that is possible to be achieved via for loop. For examples the original table would have a column d and column d_x, also column e and e_x etc.

1 Answer 1

1

You could use groupby along axis=1, grouped by the first element of splitting columns names:

for _, data in df.groupby(df.columns.str.split('_').str[0], axis=1):
    data.plot()

[out]

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.