My dataframe contains number of matches for given fixtures, but only for home matches for given team (i.e. number of matches for Argentina-Uruguay matches is 97, but for Uruguay-Argentina this number is 80). In short I want to sum both numbers of home matches for given teams, so that I have the total number of matches between the teams concerned. The dataframe's top 30 rows looks like this:
most_often = mc.groupby(["home_team", "away_team"]).size().reset_index(name="how_many").sort_values(by=['how_many'], ascending = False)
most_often = most_often.reset_index(drop=True)
most_often.head(30)
home_team away_team how_many
0 Argentina Uruguay 97
1 Uruguay Argentina 80
2 Austria Hungary 69
3 Hungary Austria 68
4 Kenya Uganda 65
5 Argentina Paraguay 64
6 Belgium Netherlands 63
7 Netherlands Belgium 62
8 England Scotland 59
9 Argentina Brazil 58
10 Brazil Paraguay 58
11 Scotland England 58
12 Norway Sweden 56
13 England Wales 54
14 Sweden Denmark 54
15 Wales Scotland 54
16 Denmark Sweden 53
17 Argentina Chile 53
18 Scotland Wales 52
19 Scotland Northern Ireland 52
20 Sweden Norway 51
21 Wales England 50
22 England Northern Ireland 50
23 Wales Northern Ireland 50
24 Chile Uruguay 49
25 Northern Ireland England 49
26 Brazil Argentina 48
27 Brazil Chile 48
28 Brazil Uruguay 47
29 Chile Peru 46
In turn, I mean something like this
0 Argentina Uruguay 177
1 Uruguay Argentina 177
2 Austria Hungary 137
3 Hungary Austria 137
4 Kenya Uganda 107
5 Uganda Kenya 107
6 Belgium Netherlands 105
7 Netherlands Belgium 105
But this is only an example, I want to apply it for every team, which I have on dataframe.
What should I do?
Ok, you can follow steps below.
Here is the initial df.
home_team away_team how_many
0 Argentina Uruguay 97
1 Uruguay Argentina 80
2 Austria Hungary 69
3 Hungary Austria 68
4 Kenya Uganda 65
Here you need to create a siorted list that will be the key foraggregations.
df1['sorted_list_team'] = list(zip(df1['home_team'],df1['away_team']))
df1['sorted_list_team'] = df1['sorted_list_team'].apply(lambda x: np.sort(np.unique(x)))
home_team away_team how_many sorted_list_team
0 Argentina Uruguay 97 [Argentina, Uruguay]
1 Uruguay Argentina 80 [Argentina, Uruguay]
2 Austria Hungary 69 [Austria, Hungary]
3 Hungary Austria 68 [Austria, Hungary]
4 Kenya Uganda 65 [Kenya, Uganda]
Here you will covert this list to tuple and turn it able to be aggregations.
def converter(list):
return (*list, )
df1['sorted_list_team'] = df1['sorted_list_team'].apply(converter)
df_sum = df1.groupby(['sorted_list_team']).agg({'how_many':'sum'}).reset_index()
sorted_list_team how_many
0 (Argentina, Brazil) 106
1 (Argentina, Chile) 53
2 (Argentina, Paraguay) 64
3 (Argentina, Uruguay) 177
4 (Austria, Hungary) 137
Do the aggregation to make a sum of 'how_many' values in another dataframe that i call 'df_sum'.
df_sum = df1.groupby(['sorted_list_team']).agg({'how_many':'sum'}).reset_index()
sorted_list_team how_many
0 (Argentina, Brazil) 106
1 (Argentina, Chile) 53
2 (Argentina, Paraguay) 64
3 (Argentina, Uruguay) 177
4 (Austria, Hungary) 137
And merge with 'df1' to get the result of a sum, the colum 'how_many' are in both dfs, for this reason pandas rename the column of df_sum as 'how_many_y'
df1 = pd.merge(df1,df_sum[['sorted_list_team','how_many']], on='sorted_list_team',how='left').drop_duplicates()
And final step you need select only columns that you need from result df.
df1 = df1[['home_team','away_team','how_many_y']]
df1 = df1.drop_duplicates()
df1.head()
home_team away_team how_many_y
0 Argentina Uruguay 177
1 Uruguay Argentina 177
2 Austria Hungary 137
3 Hungary Austria 137
4 Kenya Uganda 65
I found a relatively straightforward thing that hopefully does what you want, but is slightly different than your desired output. Your output has what looks like repetitive information where we aren't caring anymore about home-vs-away team but just want the game counts, and so let's get rid of that distinction (if we can...).
If we make a new column that combines the values from home_team and away_team in the same order each time, we can just do a sum on the how_many where that new column matches
df['teams'] = pd.Series(map('-'.join,np.sort(df[['home_team','away_team']],axis=1)))
# this creates values like 'Argentina-Brazil' and 'Chile-Peru'
df[['how_many','teams']].groupby('teams').sum()
This code gave me the following:
how_many
teams
Argentina-Brazil 106
Argentina-Chile 53
Argentina-Paraguay 64
Argentina-Uruguay 177
Austria-Hungary 137
Belgium-Netherlands 125
Brazil-Chile 48
Brazil-Paraguay 58
Brazil-Uruguay 47
Chile-Peru 46
Chile-Uruguay 49
Denmark-Sweden 107
England-Northern Ireland 99
England-Scotland 117
England-Wales 104
Kenya-Uganda 65
Northern Ireland-Scotland 52
Northern Ireland-Wales 50
Norway-Sweden 107
Scotland-Wales 106
I have the following pandas dataframe, only showing one column
0 Atlantic Division
1 Tampa Bay Lightning*
2 Boston Bruins*
3 Toronto Maple Leafs*
4 Florida Panthers
5 Detroit Red Wings
6 Montreal Canadiens
7 Ottawa Senators
8 Buffalo Sabres
9 Metropolitan Division
10 Washington Capitals*
11 Pittsburgh Penguins*
12 Philadelphia Flyers*
13 Columbus Blue Jackets*
14 New Jersey Devils*
15 Carolina Hurricanes
16 New York Islanders
17 New York Rangers
18 Central Division
19 Nashville Predators*
20 Winnipeg Jets*
21 Minnesota Wild*
22 Colorado Avalanche*
23 St. Louis Blues
24 Dallas Stars
25 Chicago Blackhawks
26 Pacific Division
27 Vegas Golden Knights*
28 Anaheim Ducks*
29 San Jose Sharks*
30 Los Angeles Kings*
31 Calgary Flames
32 Edmonton Oilers
33 Vancouver Canucks
34 Arizona Coyotes
35 Atlantic Division
36 Montreal Canadiens*
37 Ottawa Senators*
38 Boston Bruins*
39 Toronto Maple Leafs*
40 Tampa Bay Lightning
41 Florida Panthers
42 Detroit Red Wings
43 Buffalo Sabres
44 Metropolitan Division
45 Washington Capitals*
46 Pittsburgh Penguins*
47 Columbus Blue Jackets*
48 New York Rangers*
49 New York Islanders
50 Philadelphia Flyers
51 Carolina Hurricanes
52 New Jersey Devils
53 Central Division
54 Chicago Blackhawks*
55 Minnesota Wild*
56 St. Louis Blues*
57 Nashville Predators*
58 Winnipeg Jets
59 Dallas Stars
60 Colorado Avalanche
61 Pacific Division
62 Anaheim Ducks*
63 Edmonton Oilers*
64 San Jose Sharks*
65 Calgary Flames*
66 Los Angeles Kings
67 Arizona Coyotes
68 Vancouver Canucks
69 Atlantic Division
70 Florida Panthers*
71 Tampa Bay Lightning*
72 Detroit Red Wings*
73 Boston Bruins
74 Ottawa Senators
75 Montreal Canadiens
76 Buffalo Sabres
77 Toronto Maple Leafs
78 Metropolitan Division
79 Washington Capitals*
80 Pittsburgh Penguins*
81 New York Rangers*
82 New York Islanders*
83 Philadelphia Flyers*
84 Carolina Hurricanes
85 New Jersey Devils
86 Columbus Blue Jackets
87 Central Division
88 Dallas Stars*
89 St. Louis Blues*
90 Chicago Blackhawks*
91 Nashville Predators*
92 Minnesota Wild*
93 Colorado Avalanche
94 Winnipeg Jets
95 Pacific Division
96 Anaheim Ducks*
97 Los Angeles Kings*
98 San Jose Sharks*
99 Arizona Coyotes
100 Calgary Flames
101 Vancouver Canucks
102 Edmonton Oilers
103 Atlantic Division
104 Montreal Canadiens*
105 Tampa Bay Lightning*
106 Detroit Red Wings*
107 Ottawa Senators*
108 Boston Bruins
109 Florida Panthers
110 Toronto Maple Leafs
111 Buffalo Sabres
112 Metropolitan Division
113 New York Rangers*
114 Washington Capitals*
115 New York Islanders*
116 Pittsburgh Penguins*
117 Columbus Blue Jackets
118 Philadelphia Flyers
119 New Jersey Devils
120 Carolina Hurricanes
121 Central Division
122 St. Louis Blues*
123 Nashville Predators*
124 Chicago Blackhawks*
125 Minnesota Wild*
126 Winnipeg Jets*
127 Dallas Stars
128 Colorado Avalanche
129 Pacific Division
130 Anaheim Ducks*
131 Vancouver Canucks*
132 Calgary Flames*
133 Los Angeles Kings
134 San Jose Sharks
135 Edmonton Oilers
136 Arizona Coyotes
137 Atlantic Division
138 Boston Bruins*
139 Tampa Bay Lightning*
140 Montreal Canadiens*
141 Detroit Red Wings*
142 Ottawa Senators
143 Toronto Maple Leafs
144 Florida Panthers
145 Buffalo Sabres
146 Metropolitan Division
147 Pittsburgh Penguins*
148 New York Rangers*
149 Philadelphia Flyers*
150 Columbus Blue Jackets*
151 Washington Capitals
152 New Jersey Devils
153 Carolina Hurricanes
154 New York Islanders
155 Central Division
156 Colorado Avalanche*
157 St. Louis Blues*
158 Chicago Blackhawks*
159 Minnesota Wild*
160 Dallas Stars*
161 Nashville Predators
162 Winnipeg Jets
163 Pacific Division
164 Anaheim Ducks*
165 San Jose Sharks*
166 Los Angeles Kings*
167 Phoenix Coyotes
168 Vancouver Canucks
169 Calgary Flames
170 Edmonton Oilers
Name: team, dtype: object
I need to create one additional column with the city name.
At first look the regex would be simple (the first word) should be the city name, and the rest is the team name.
However some cities have 2 words (Los Angeles, St Louis ,etc)
Is there a possibility to do this with regex or it has to be done manually?
Update: I tried the following:
nhl_df['city']=nhl_df['team'].str.extract(r'^(?:([\w.]{1,5}\s\w+)|(\w+)|)(?:\s\w+)+\*?$')
But I get this error:
ValueError: Wrong number of items passed 2, placement implies 1
You can try something like that:
^(?:([\w.]{1,5}\s\w+)|(\w+)|)(?:\s\w+)+\*?$
Here you should look for city name in first or second group.
This pattern uses assumption that first part of two-word city names has no more than 5 symbols. The result might not be so clean, but seems to work fine on given example.
You can use
^([\w.]{1,5}(?:\s\w+)?\w*)
See the regex demo. Details:
^ - start of string
([\w.]{1,5}(?:\s\w+)?\w*) - Capturing group 1:
[\w.]{1,5} - one to five word or dot chars
(?:\s\w+)? - an optional occurrence of a whitespace and then one or more word chars
\w* - zero or more word chars.
Pandas test:
import pandas as pd
nhl_df = pd.DataFrame({"team":["Atlantic Division","Tampa Bay Lightning*","Boston Bruins*","Toronto Maple Leafs*","Florida Panthers","Detroit Red Wings","Montreal Canadiens","Ottawa Senators","Buffalo Sabres","Metropolitan Division","Washington Capitals*","Pittsburgh Penguins*","Philadelphia Flyers*","Columbus Blue Jackets*","New Jersey Devils*","Carolina Hurricanes","New York Islanders","New York Rangers","Central Division","Nashville Predators*","Winnipeg Jets*","Minnesota Wild*","Colorado Avalanche*","St. Louis Blues","Dallas Stars","Chicago Blackhawks","Pacific Division","Vegas Golden Knights*","Anaheim Ducks*","San Jose Sharks*","Los Angeles Kings*","Calgary Flames","Edmonton Oilers","Vancouver Canucks","Arizona Coyotes","Atlantic Division","Montreal Canadiens*","Ottawa Senators*","Boston Bruins*","Toronto Maple Leafs*","Tampa Bay Lightning","Florida Panthers","Detroit Red Wings","Buffalo Sabres","Metropolitan Division","Washington Capitals*","Pittsburgh Penguins*","Columbus Blue Jackets*","New York Rangers*","New York Islanders","Philadelphia Flyers","Carolina Hurricanes","New Jersey Devils","Central Division","Chicago Blackhawks*","Minnesota Wild*","St. Louis Blues*","Nashville Predators*","Winnipeg Jets","Dallas Stars","Colorado Avalanche","Pacific Division","Anaheim Ducks*","Edmonton Oilers*","San Jose Sharks*","Calgary Flames*","Los Angeles Kings","Arizona Coyotes","Vancouver Canucks","Atlantic Division","Florida Panthers*","Tampa Bay Lightning*","Detroit Red Wings*","Boston Bruins","Ottawa Senators","Montreal Canadiens","Buffalo Sabres","Toronto Maple Leafs","Metropolitan Division","Washington Capitals*","Pittsburgh Penguins*","New York Rangers*","New York Islanders*","Philadelphia Flyers*","Carolina Hurricanes","New Jersey Devils","Columbus Blue Jackets","Central Division","Dallas Stars*","St. Louis Blues*","Chicago Blackhawks*","Nashville Predators*","Minnesota Wild*","Colorado Avalanche","Winnipeg Jets","Pacific Division","Anaheim Ducks*","Los Angeles Kings*","San Jose Sharks*","Arizona Coyotes","Calgary Flames","Vancouver Canucks","Edmonton Oilers","Atlantic Division","Montreal Canadiens*","Tampa Bay Lightning*","Detroit Red Wings*","Ottawa Senators*","Boston Bruins","Florida Panthers","Toronto Maple Leafs","Buffalo Sabres","Metropolitan Division","New York Rangers*","Washington Capitals*","New York Islanders*","Pittsburgh Penguins*","Columbus Blue Jackets","Philadelphia Flyers","New Jersey Devils","Carolina Hurricanes","Central Division","St. Louis Blues*","Nashville Predators*","Chicago Blackhawks*","Minnesota Wild*","Winnipeg Jets*","Dallas Stars","Colorado Avalanche","Pacific Division","Anaheim Ducks*","Vancouver Canucks*","Calgary Flames*","Los Angeles Kings","San Jose Sharks","Edmonton Oilers","Arizona Coyotes","Atlantic Division","Boston Bruins*","Tampa Bay Lightning*","Montreal Canadiens*","Detroit Red Wings*","Ottawa Senators","Toronto Maple Leafs","Florida Panthers","Buffalo Sabres","Metropolitan Division","Pittsburgh Penguins*","New York Rangers*","Philadelphia Flyers*","Columbus Blue Jackets*","Washington Capitals","New Jersey Devils","Carolina Hurricanes","New York Islanders","Central Division","Colorado Avalanche*","St. Louis Blues*","Chicago Blackhawks*","Minnesota Wild*","Dallas Stars*","Nashville Predators","Winnipeg Jets","Pacific Division","Anaheim Ducks*","San Jose Sharks*","Los Angeles Kings*","Phoenix Coyotes","Vancouver Canucks","Calgary Flames","Edmonton Oilers"]})
nhl_df['city']=nhl_df['team'].str.extract(r'^([\w.]{1,5}(?:\s\w+)?\w*)')
>>> nhl_df
team city
0 Atlantic Division Atlantic
1 Tampa Bay Lightning* Tampa Bay
2 Boston Bruins* Boston
3 Toronto Maple Leafs* Toronto
4 Florida Panthers Florida
.. ... ...
166 Los Angeles Kings* Los Angeles
167 Phoenix Coyotes Phoenix
168 Vancouver Canucks Vancouver
169 Calgary Flames Calgary
170 Edmonton Oilers Edmonton
^\S+(?=\s\S+$)
This regex gives you the first word of all teamnames that only consist of two words. The others you have to sort manually, because there is no way to tell just by pattern if the middle word is part of the city or the teamname.
Try using the below regex
/(\d*\s*)([a-zA-Z\s]*)(\s)(\b([a-zA-z\*]*))$/
Checkthis
function Replace(str) {
var result = str.replace(/(\d*\s*)([a-zA-Z\s]*)(\s)(\b([a-zA-z\*]*))$/gim, function (a, $1, $2, $3, $4) {
return `${$2}--${$4}`;
});
return result;
}
This question already has answers here:
Get the row(s) which have the max value in groups using groupby
(15 answers)
Closed 3 years ago.
I want to find the player with max overall rating in each position. What is the best and compact way to do it in pandas?.
Name Overall Potential Club Position
L. Messi 94 94 FC Barcelona RF
Ronaldo 94 94 Juventus ST
Neymar Jr 92 93 Paris Saint-Germain LW
De Gea 91 93 Manchester United GK
K. De Bruyne 91 92 Manchester City RCM
E. Hazard 91 91 Chelsea LF
L. Modrić 91 91 Real Madrid RCM
L. Suárez 91 91 FC Barcelona RS
Sergio Ramos 91 91 Real Madrid RCB
J. Oblak 90 93 Atlético Madrid GK
R. Lewandowski 90 90 FC Bayern München ST
T. Kroos 90 90 Real Madrid LCM
I have tried:
fifa.groupby(by = ["Position"])['Overall'].max()
followed by
fifa.loc[(fifa["Position"] == "CAM") & (fifa['Overall'] == 89),:]
But since there are so many categories in Position, it's a tedious task.
You can try this:
df[df["Overall"]==df["Overall"].max()]
This will help.
Use DataFrame.drop_duplicates(assuming Overall column is sorted):
df = df.drop_duplicates(subset=['Position'], keep='first')
print(df)
Name Overall Potential Club Position
0 L. Messi 94 94 FC Barcelona RF
1 Ronaldo 94 94 Juventus ST
2 Neymar Jr 92 93 Paris Saint-Germain LW
3 De Gea 91 93 Manchester United GK
4 K. De Bruyne 91 92 Manchester City RCM
5 E. Hazard 91 91 Chelsea LF
7 L. Suárez 91 91 FC Barcelona RS
8 Sergio Ramos 91 91 Real Madrid RCB
11 T. Kroos 90 90 Real Madrid LCM
You could merge your intermediate result with the original dataframe to get the full rows:
pd.DataFrame(df.groupby('Position')['Overall'].max()).reset_index().merge(df,
on=['Position', 'Overall'])
It gives:
Position Overall Name Potential Club
0 GK 91 De Gea 93 Manchester United
1 LCM 90 T. Kroos 90 Real Madrid
2 LF 91 E. Hazard 91 Chelsea
3 LW 92 Neymar Jr 93 Paris Saint-Germain
4 RCB 91 Sergio Ramos 91 Real Madrid
5 RCM 91 K. De Bruyne 92 Manchester City
6 RCM 91 L. Modrić 91 Real Madrid
7 RF 94 L. Messi 94 FC Barcelona
8 RS 91 L. Suárez 91 FC Barcelona
9 ST 94 Ronaldo 94 Juventus
You can note the 2 ex-aequo for RCM position.
I've downloaded some HTML files I am wanting to parse. I was able to parse the files but now I want to make some lists so I can make a scatter plot. I'm totally new to Python so I am not sure how to make these into lists.
I tried setting a variable equal to the text I got from the the column.
for y in range (1977, 2020, 1):
tmp = random.random()*5.0
print ('Sleep for ', tmp, ' seconds')
time.sleep(tmp)
url = 'https://www.basketball-reference.com/teams/IND/'+ str(y) +'_games.html'
print ('Download from :', url)
#dowlnload
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req).read()
fileout = 'YEARS/'+str(y)+'.html'
print ('Save to : ', fileout, '\n')
#save file to disk
f = open(fileout,'w')
f.write(html.decode('utf-8'))
f.close()
#parse
for year in range (1977, 2019, 1):
filein = 'YEARS/' + str(year) + '.html'
soup = BeautifulSoup(open(filein), 'lxml')
entries = soup.find_all('tr', attrs={'class' : ''})
for entry in entries:
#print entry
columns = entry.find_all('td')
if len (columns)>4 :
#print ('C0: ', columns[4])
where = columns[4].get_text()
#print ('C1: ', columns[5])
opponent = columns[5].get_text()
#print ('C2: ', columns[6])
WL = columns[6].get_text()
#print ('C3: ', columns[8])
PacerScore = columns[8].get_text()
#print ('C4: ', columns[9])
OpponentScore = columns[9].get_text()
tt = where+'|::|'+opponent+'|::|'+WL+'|::|'+PacerScore+'|::|'+OpponentScore
print (tt)
x = PacerScore
y = OpponentScore
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
I tried using read_html from pandas as well but I was screwing something up and could not get it to work. It kept telling me feature not found.
#parse
for y in range (1977, 2019, 1):
filein = 'YEARS/' + str(y) + '.html'
soup = BeautifulSoup(open(filein), 'r')
table = BeautifulSoup(open('YEARS/' + str(y) + '.html','r').read()).find('table')
df = pd.read_html(table)
Any advice or pointers would be greatly appreciated.
If you are using pandas' .read_html(), you don't need to use beautifulsoup to find the table tags. Pandas does that for you. You're also doing a ton of work to first save the html, then parse the html. Why not parse the html straight a way, and then if you wanted, just save that table?
Then you can plot using the table.
import requests
import pandas as pd
import numpy as np
import time
import random
headers={'User-Agent': 'Mozilla/5.0'}
for year in range (1977, 2020, 1):
tmp = random.random()*5.0
print ('Sleep for ', tmp, ' seconds')
time.sleep(tmp)
url = 'https://www.basketball-reference.com/teams/IND/'+ str(year) +'_games.html'
response = requests.get(url, headers=headers)
tables = pd.read_html(url)
table = tables[0]
table = table[table['G'] != 'G']
table = table[['Unnamed: 5', 'Opponent','Unnamed: 7','Tm','Opp']]
table.columns = ['Where','Opponent','WL','PacerScore','OpponentScore']
table['Where'] = np.where(table.Where == '#', 'Away', 'Home')
print ('Download table from :', url)
table.to_csv('YEARS/' + str(year) + '.csv')
Your table will look like this, they you can just do:
x = table['PacerScore']
y = table['OpponentScore']
to get your x and y values for your scatter plot.
Output:
print (table.to_string())
Where Opponent WL PacerScore OpponentScore Season
0 Home Memphis Grizzlies W 111 83 2019
1 Away Milwaukee Bucks L 101 118 2019
2 Home Brooklyn Nets W 132 112 2019
3 Away Minnesota Timberwolves L 91 101 2019
4 Away San Antonio Spurs W 116 96 2019
5 Away Cleveland Cavaliers W 119 107 2019
6 Home Portland Trail Blazers L 93 103 2019
7 Away New York Knicks W 107 101 2019
8 Away Chicago Bulls W 107 105 2019
9 Home Boston Celtics W 102 101 2019
10 Home Houston Rockets L 94 98 2019
11 Home Philadelphia 76ers L 94 100 2019
12 Away Miami Heat W 110 102 2019
13 Away Houston Rockets L 103 115 2019
14 Home Miami Heat W 99 91 2019
15 Home Atlanta Hawks W 97 89 2019
16 Home Utah Jazz W 121 94 2019
17 Away Charlotte Hornets L 109 127 2019
18 Home San Antonio Spurs L 100 111 2019
19 Away Utah Jazz W 121 88 2019
21 Away Phoenix Suns W 109 104 2019
22 Away Los Angeles Lakers L 96 104 2019
23 Away Sacramento Kings L 110 111 2019
24 Home Chicago Bulls W 96 90 2019
25 Away Orlando Magic W 112 90 2019
26 Home Sacramento Kings W 107 97 2019
27 Home Washington Wizards W 109 101 2019
28 Home Milwaukee Bucks W 113 97 2019
29 Away Philadelphia 76ers W 113 101 2019
30 Home New York Knicks W 110 99 2019
31 Home Cleveland Cavaliers L 91 92 2019
32 Away Toronto Raptors L 96 99 2019
33 Away Brooklyn Nets W 114 106 2019
34 Home Washington Wizards W 105 89 2019
35 Away Atlanta Hawks W 129 121 2019
36 Home Detroit Pistons W 125 88 2019
37 Home Atlanta Hawks W 116 108 2019
38 Away Chicago Bulls W 119 116 2019
39 Away Toronto Raptors L 105 121 2019
40 Away Cleveland Cavaliers W 123 115 2019
42 Away Boston Celtics L 108 135 2019
43 Away New York Knicks W 121 106 2019
44 Home Phoenix Suns W 131 97 2019
45 Home Philadelphia 76ers L 96 120 2019
46 Home Dallas Mavericks W 111 99 2019
47 Home Charlotte Hornets W 120 95 2019
48 Home Toronto Raptors W 110 106 2019
49 Away Memphis Grizzlies L 103 106 2019
50 Home Golden State Warriors L 100 132 2019
51 Away Washington Wizards L 89 107 2019
52 Away Orlando Magic L 100 107 2019
53 Away Miami Heat W 95 88 2019
54 Away New Orleans Pelicans W 109 107 2019
55 Home Los Angeles Lakers W 136 94 2019
56 Home Los Angeles Clippers W 116 92 2019
57 Home Cleveland Cavaliers W 105 90 2019
58 Home Charlotte Hornets W 99 90 2019
59 Home Milwaukee Bucks L 97 106 2019
60 Home New Orleans Pelicans W 126 111 2019
61 Away Washington Wizards W 119 112 2019
63 Away Detroit Pistons L 109 113 2019
64 Away Dallas Mavericks L 101 110 2019
65 Home Minnesota Timberwolves W 122 115 2019
66 Home Orlando Magic L 112 117 2019
67 Home Chicago Bulls W 105 96 2019
68 Away Milwaukee Bucks L 98 117 2019
69 Away Philadelphia 76ers L 89 106 2019
70 Home New York Knicks W 103 98 2019
71 Home Oklahoma City Thunder W 108 106 2019
72 Away Denver Nuggets L 100 102 2019
73 Away Portland Trail Blazers L 98 106 2019
74 Away Los Angeles Clippers L 109 115 2019
75 Away Golden State Warriors L 89 112 2019
76 Home Denver Nuggets NaN NaN NaN 2019
77 Away Oklahoma City Thunder NaN NaN NaN 2019
78 Away Boston Celtics NaN NaN NaN 2019
79 Home Orlando Magic NaN NaN NaN 2019
80 Home Detroit Pistons NaN NaN NaN 2019
81 Away Detroit Pistons NaN NaN NaN 2019
82 Home Boston Celtics NaN NaN NaN 2019
84 Home Brooklyn Nets NaN NaN NaN 2019
85 Away Atlanta Hawks NaN NaN NaN 2019
I have Fifa dataset and it includes information about football players. One of the features of this dataset is the value of football players but it is in string form such as "$300K" or "$50M". How can I delete simply these euro and "M, K" symbol and write their values in same units?
import numpy as np
import pandas as pd
location = r'C:\Users\bemrem\Desktop\Python\fifa\fifa_dataset.csv'
_dataframe = pd.read_csv(location)
_dataframe = _dataframe.dropna()
_dataframe = _dataframe.reset_index(drop=True)
_dataframe = _dataframe[['Name', 'Value', 'Nationality', 'Age', 'Wage',
'Overall', 'Potential']]
_array = ['Belgium', 'France', 'Brazil', 'Croatia', 'England',' Portugal',
'Uruguay', 'Switzerland', 'Spain', 'Denmark']
_dataframe = _dataframe.loc[_dataframe['Nationality'].isin(_array)]
_dataframe = _dataframe.reset_index(drop=True)
print(_dataframe.head())
print()
print(_dataframe.tail())
I tried to convert this Value column but I failed. This is what I get
Name Value Nationality Age Wage Overall Potential
0 Neymar €123M Brazil 25 €280K 92 94
1 L. Suárez €97M Uruguay 30 €510K 92 92
2 E. Hazard €90.5M Belgium 26 €295K 90 91
3 Sergio Ramos €52M Spain 31 €310K 90 90
4 K. De Bruyne €83M Belgium 26 €285K 89 92
Name Value Nationality Age Wage Overall Potential
4931 A. Kilgour €40K England 19 €1K 47 56
4932 R. White €60K England 18 €2K 47 65
4933 T. Sawyer €50K England 18 €1K 46 58
4934 J. Keeble €40K England 18 €1K 46 56
4935 J. Lundstram €60K England 18 €1K 46 64
But I want to my output looks like this:
Name Value Nationality Age Wage Overall Potential
0 Neymar 123 Brazil 25 €280K 92 94
1 L. Suárez 97 Uruguay 30 €510K 92 92
2 E. Hazard 90.5 Belgium 26 €295K 90 91
3 Sergio Ramos 52 Spain 31 €310K 90 90
4 K. De Bruyne 83 Belgium 26 €285K 89 92
Name Value Nationality Age Wage Overall Potential
4931 A. Kilgour 0.04 England 19 €1K 47 56
4932 R. White 0.06 England 18 €2K 47 65
4933 T. Sawyer 0.05 England 18 €1K 46 58
4934 J. Keeble 0.04 England 18 €1K 46 56
4935 J. Lundstram 0.06 England 18 €1K 46 64
I do not have enough reputation to flag an answer as a duplicate. However, I believe that this will solve your particular question in addition to providing a solution if there is no "K" or "M" in your string.
You will also need to replace $ with € in the regex.
Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe