Можно использовать .dropna()
с аргументом subset
с последующим удалением $
и преобразованием во float
через .astype()
df = pd.read_csv(r'c:\test\worldwide internet prices in 2022 - IN 2022.csv')
df = df.dropna(subset=['Cheapest 1GB for 30 days (USD)', 'Most expensive 1GB (USD)'])
df['Cheapest 1GB for 30 days (USD)'] = df['Cheapest 1GB for 30 days (USD)'].str.lstrip('$').astype(float)
df['Most expensive 1GB (USD)'] = df['Most expensive 1GB (USD)'].str.lstrip('$').astype(float)
print(df)
Country code Name Continental region NO. OF Internet Plans Average price of 1GB (USD) Cheapest 1GB for 30 days (USD) Most expensive 1GB (USD) Average price of 1GB (USD at the start of 2021) Average price of 1GB (USD – at start of 2020)
0 IL Israel NEAR EAST 27.0 $0.05 0.02 20.95 $0.11 $0.90
1 KG Kyrgyzstan CIS (FORMER USSR) 20.0 $0.15 0.10 7.08 $0.21 $0.27
2 FJ Fiji OCEANIA 18.0 $0.19 0.05 0.85 $0.59 $3.57
3 IT Italy WESTERN EUROPE 29.0 $0.27 0.09 3.54 $0.43 $1.73
4 SD Sudan SUB-SAHARAN AFRICA 33.0 $0.27 0.03 0.92 $0.63 $0.68
.. ... ... ... ... ... ... ... ... ...
225 MW Malawi SUB-SAHARAN AFRICA 5.0 $25.46 20.37 25.46 $27.41 $3.59
226 ST São Tomé and Príncipe SUB-SAHARAN AFRICA 16.0 $30.97 13.27 121.65 $28.26 $5.33
227 SH Saint Helena SUB-SAHARAN AFRICA 4.0 $39.87 38.57 51.42 $52.50 $6.04
228 FK Falkland Islands SOUTH AMERICA 4.0 $44.56 29.71 82.26 $40.41 $55.47
229 GQ Equatorial Guinea SUB-SAHARAN AFRICA 4.0 $49.67 45.16 63.22 $12.78 $47.39
[230 rows x 9 columns]
Если нужно все колонки с ценами сделать числовыми, то можно так:
df = pd.read_csv(r'c:\test\worldwide internet prices in 2022 - IN 2022.csv')
df = df.dropna(subset=['Cheapest 1GB for 30 days (USD)', 'Most expensive 1GB (USD)'])
df = df.replace({'NO PACKAGES': np.nan})
df.loc[:, 'Average price of 1GB (USD)':] = df.loc[:, 'Average price of 1GB (USD)':].apply(
lambda x: x.str.lstrip('$').astype(float))
print(df)