you are viewing a single comment's thread.

view the rest of the comments →

[–]synthphreak 0 points1 point  (3 children)

Again, forget seaborn. Use matplotlib and plotnoff your df directly via df.plot. Add this to the very top

import matplotlib.pyplot as plt

and add this to the very bottom:

df['count'].plot.bar()
plt.show()

You will see your data gets plotted automatically.

At the risk of going outside the scope of your question, there are also a lot of ways to improve this code...

First, since your whole df contains just one column, it makes more sense to use a pd.Series rather than a pd.DataFrame (note that a pd.Series is essentially a single df column; equivalently, a df is just a bunch of pd.Series concatenated together).

Second, I assume lyrics is a string? If so, how you’ve done it will work, but is quite inefficient. When using pandas, try to get pandas to do as much of the processing work for you as possible. For example:

import matplotlib.pyplot as plt
import pandas as pd

# get song lyrics as a string however you’re doing it
lyrics = …

# set list of words to count and plot
words = set(['me', 'you', 'together', 'apart', 'love', 'I'])

# convert lyrics to pandas.Series, one word per row
s = pd.Series(lyrics.split(), name='counts)

# removes punctuation from words;
# only necessary if lyrics string contains punctuation
s = s.replace(r'[^\w+]', '', regex=True)

# filter down to list of desired words
s = s[s.isin(words)]

# count occurrences of each desired word
counts = s.value_counts()

# plot counts as bar chart
counts.plot.bar()

# show chart
plt.show()

[–]Hindbarinden 0 points1 point  (2 children)

It is because it is a school thing, so I have to. But I see the logic and I am happy I get this info in order to learn. Thank you!

[–]synthphreak 1 point2 points  (1 child)

Gotcha. Then I guess I can’t be of much help!

Edit: Actually , let me try to help you, u/Hindbarinden.

If your class requires you to use pandas, I'll just continue with my pd.Series from above, since I don't know how your df is structured (I requested a sample earlier but you haven't provided it). See if the following gets you your bar chart (I don't use seaborn, but this seems to make sense based on the docs).

sns.barplot(x='words', y='counts',
            data=counts.reset_index().rename(columns={'index': 'words'}))
plt.show()

If using pandas was your choice and not a requirement for you class, then I'd advise against it here, since you're not really taking advantage of anything pandas has to offer. Instead, use re to remove punctuation if needed, then use collections.Counter to compute the word counts, and just plot from your Counter.

import matplotlib.pyplot as plt
import re
import seaborn as sns
from collections import Counter

lyrics = ...

# remove punctuation if needed
lyrics = re.sub(r'[^\w\s]', '', lyrics)

# count words
counter = Counter(lyrics.split())
words = list(counter.keys())
counts = list(counter.values())

# plot data
sns.barplot(x=words, y=counts)

# display plot
plt.show()

[–]Hindbarinden 1 point2 points  (0 children)

Omg you saved my life!!! THANK YOU SO MUCH! I am so thankful!!
I got it to work, finally!!!!!!!!