简体   繁体   中英

For each unique element in a column, create a plot with it's corresponding features

I believe that my problem is really straightforward and there must be a really easy way to solve this issue with pandas that I am still not aware of. I don't really want to create new dataframes. I have already spent some time trying to figure out by myself but could not reach a result.

A simple example might be a better way to explain my goal: I have four different unique values, indicated by the code below, on the column C. What I want is to create a for loop where for each of this unique values (Value_1, Value_2, Value_3, Value_4), plot a graph which the 'x' is the corresponding feature in Column A for that value and 'y' is the corresponding column B.

import pandas as pd
data = {'Column A': [100,200,300,400,500,500,500,300],
    'Column B': [1,1,2,2,3,3,0,2], 
    'Column C': ["Value_1", "Value_2", "Value_3", "Value_4", "Value_1", "Value_2", "Value_3", "Value_4"]}
df = pd.DataFrame(data, columns=['Column A','Column B', 'Column C'])

I would expect afterwards a result, as for this case where I would receive four plots, where each represents one of the values of the column C and the correspondings features of column A and B.

I have tried the following:

for d in df.groupby(df['Column C']):
df.plot.scatter(x='Column A', y='Column B', marker='.')
plt.show()

However by doing so, I am not able to really achieve what I want as all the points are plotted in this graph, without the filtering that I want to implement with column C.

Hope that I could succinct and precise. Wish you the best and thanks for the attention.

I guess you can just loop over the unique values with this:

for val in df['Column C'].unique():
    sdf = df.loc[df['Column C']==val]
    sdf.plot(x='Column A', y='Column B', label='C = {}'.format(val))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM