-2

How to read string row in python?

I got a football csv file.

https://www.football-data.co.uk/mmz4281/1920/F1.csv I would like to retrieve all the lines where there is the PS Germain.

import pandas as pd
df = pd.read_csv('F1.csv')
dh = df[['Date','HomeTeam','AwayTeam','FTHG','FTAG']]
dh.head()
maceo bs
  • 59
  • 8

4 Answers4

2

You need to select rows from your Pandas DataFrame. You can use the following logic to select rows from Pandas DataFrame based on specific conditions:

df.loc[df['column name'] condition]

In pratice that means:

result = df.loc[df['HomeTeam'] == 'PS Germain']

You're getting the result with:

print(result)

Of course you can use more conditions and combine them.

squeezer44
  • 560
  • 2
  • 17
1

'Paris SG' HomeTeam or AwayTeam:

import pandas as pd
df = pd.read_csv('F1.csv')
dh = df[['Date','HomeTeam','AwayTeam','FTHG','FTAG']]

index_list = dh[(dh['HomeTeam'] == 'Paris SG') | (dh['AwayTeam'] == 'Paris SG')].index.tolist()
dh_final = dh.iloc[index_list]
0

Filtering where Paris SG is in the HomeTeam column:

import pandas as pd

df = pd.read_csv('https://www.football-data.co.uk/mmz4281/1920/F1.csv')
df = df[df['HomeTeam'] == 'Paris SG']
dh762
  • 2,259
  • 4
  • 25
  • 44
0

Try the below sample:

for ind in df.index:
  if(df['HomeTeam'][ind]=='PS Germain'):
    #Do the needfull
Kokul Jose
  • 1,384
  • 2
  • 14
  • 26