Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

python - Plot key count per unique value count in pandas

I have a set of data from which I want to plot the number of keys per unique id count (x=unique_id_count, y=key_count), and I'm trying to learn how to take advantage of pandas.

In this case:

unique_ids 1 = key count 2

unique_ids 2 = key count 1

from pandas import *
key_items = ("a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c")
id_data = ("X", "X", "X", "X", "X", "X", "X", "Y", "Y", "Y", "X", "X", "X")

df = DataFrame({'keys': key_items, 'ids': id_data})

I've managed to mangle the data into what I want by pulling out the data from the dataframe and restructuring it, and rebuilding a new dataframe. In this case it's probably better to do it all in python without pandas...

unique_values = defaultdict(list)
for items in df.itertuples(index=False):
    key = items[1]
    v = items[0]
    unique_values[key].append(v)

unique_values_count = {}
for k, values in unique_values.iteritems():
    unique_values_count[k] = [len(set(values))]

# reformat for plotting
key_col = ("a", "b", "c")
id_col = [unique_values_count[k][0] for k in key_col]



df2 = DataFrame({"keys":key_col, "unique_id_count": id_col})
df2.groupby("unique_id_count").size().plot(kind="bar")

Is there a better way to do this more directly using the initial dataframe?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about just directly use value_counts()

pd.value_counts(df['ids']).plot.bar()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...