0

I currently have my csv file displaying on python:

df = pd.read_csv("Desktop\Assignment\World Cup 2018.csv")
df.head()

Here I can see that my data has been opened and the unneeded columns have been removed. Now I want to use some variables named CounterVal1 (and so on) to count the amount of times a formation appears in a row.

for i in enumerate(df['home_formation']):
if i == '4-2-3-1':
    counterVal1 += 1
elif i == '4-1-4-1':
      counterVal2 += 1

performance = [counterVal1,counterVal2,counterVal3,counterVal4,counterVal5,counterVal6]

plt.bar(y_pos, performance, align='center', alpha=0.8)

However I have printed off one of these values and it appears that the data is not being searched through as show above.

My question: How do I take in the data from the CSV file, take just the column related to the formation and loop through it?

VicentVega
  • 191
  • 1
  • 4
  • 11
  • 1
    If I understand correctly, you can use `performance = df['home_formation'].value_counts()` to count every unique formation and not to iterate through 'home_formation' column. Or do you need to consider only certain formations? – Teoretic Dec 17 '18 at 15:04
  • I will be counting how many times all formations appear in the csv file and then displaying that in a bar chart – VicentVega Dec 17 '18 at 15:07
  • Possible duplicate of [count the frequency that a value occurs in a dataframe column](https://stackoverflow.com/questions/22391433/count-the-frequency-that-a-value-occurs-in-a-dataframe-column) – Ricky Kim Dec 17 '18 at 15:10

1 Answers1

2

You haven't really provided much here. So this is just guess work.

Based on your comment I'm guessing this is what you want :

df['home-formation'].value_counts().plot('bar')
Omkar Sabade
  • 765
  • 3
  • 12