如何将数据集中多个场地映射为对应国家并生成新列?
高效实现板球场到所属国家的映射
步骤1:创建场地-国家映射字典
先把所有给定场地对应的国家整理成字典,确保覆盖所有条目:
ground_to_country = { 'Auckland': 'New Zealand', 'Southampton': 'England', 'Johannesburg': 'South Africa', 'Brisbane': 'Australia', 'Bristol': 'England', 'Khulna': 'Bangladesh', 'Wellington': 'New Zealand', 'Sydney': 'Australia', 'The Oval': 'England', 'Nairobi (Gym)': 'Kenya', 'Durban': 'South Africa', 'Cape Town': 'South Africa', 'Brabourne': 'India', 'Perth': 'Australia', 'Gqeberha': 'South Africa', 'Melbourne': 'Australia', 'Christchurch': 'New Zealand', 'Karachi': 'Pakistan', 'Manchester': 'England', 'Bridgetown': 'Barbados', 'Belfast': 'England', 'King City (NW)': 'Canada', 'Hamilton': 'New Zealand', 'Colombo (RPS)': 'Sri Lanka', 'Port of Spain': 'Trinidad and Tobago', 'Centurion': 'South Africa', 'Dubai (DSC)': 'United Arab Emirates', "Lord's": 'England', 'Nottingham': 'England', 'Basseterre': 'Saint Kitts and Nevis', 'Nagpur': 'India', 'Mohali': 'India', 'Colombo (PSS)': 'Sri Lanka', 'Abu Dhabi': 'United Arab Emirates', 'Hobart': 'Australia', 'Providence': 'Guyana', 'Gros Islet': 'Saint Lucia', 'North Sound': 'Antigua and Barbuda', 'Lauderhill': 'United States', 'Harare': 'Zimbabwe', 'Birmingham': 'England', 'Cardiff': 'England', 'Bloemfontein': 'South Africa', 'Kimberley': 'South Africa', 'Adelaide': 'Australia', 'Pallekele': 'Sri Lanka', 'Mirpur': 'Bangladesh', 'Eden Gardens': 'India', 'Mombasa': 'Kenya', 'ICCA Dubai': 'United Arab Emirates', 'Hambantota': 'Sri Lanka', 'The Hague': 'Netherlands', 'Chester-le-Street': 'England', 'Chennai': 'India', 'Pune': 'India', 'Wankhede': 'India', 'East London': 'South Africa', 'Bengaluru': 'India', 'Ahmedabad': 'India', 'Sharjah': 'United Arab Emirates', 'Windhoek': 'Namibia', 'Bulawayo': 'Zimbabwe', 'Aberdeen': 'England', 'Kingstown': 'Saint Vincent and the Grenadines', 'Rajkot': 'India', 'Chattogram': 'Bangladesh', 'Kingston': 'Jamaica', 'Sylhet': 'Bangladesh', 'Roseau': 'Dominica', 'Lahore': 'Pakistan', 'Bready': 'England', 'Edinburgh': 'England', 'Dublin (Malahide)': 'Ireland', 'Dharamsala': 'India', 'Cuttack': 'India', 'Mount Maunganui': 'New Zealand', 'Ranchi': 'India', 'Visakhapatnam': 'India', 'Delhi': 'India', 'Napier': 'New Zealand', 'Kanpur': 'India', 'Geelong': 'Australia', 'Greater Noida': 'India', 'Taunton': 'England', 'Guwahati': 'India', 'Potchefstroom': 'South Africa', 'Thiruvananthapuram': 'India', 'Indore': 'India', 'Nelson': 'New Zealand', 'Dehradun': 'India', 'Rotterdam': 'Netherlands', 'Deventer': 'Netherlands', 'Amstelveen': 'Netherlands', 'Lucknow': 'India', 'Carrara': 'Australia', 'Al Amerat': 'Oman', 'ICCA 2 Dubai': 'United Arab Emirates', 'Canberra': 'Australia', 'Hyderabad': 'India', "St George's": 'Grenada', 'Rawalpindi': 'Pakistan', 'Paarl': 'South Africa', 'Dunedin': 'New Zealand', 'Coolidge': 'Anguilla', 'Leeds': 'England', 'Dublin': 'Ireland', 'Jaipur': 'India', 'Tarouba': 'Trinidad and Tobago' }
步骤2:用Pandas矢量化映射生成新列
对于大型数据集,矢量化操作是最高效的方式,避免低效的逐行循环。直接使用pandas.Series.map()方法:
import pandas as pd # 假设你的数据集为df,Ground是目标列 df['Country'] = df['Ground'].map(ground_to_country)
补充说明
- 该方法时间复杂度为O(n),常数项远小于逐行循环,处理百万级数据也能快速完成。
- 若担心存在未匹配的场地,可添加
fillna()处理缺失值:df['Country'] = df['Ground'].map(ground_to_country).fillna('Unknown')
内容的提问来源于stack exchange,提问作者UDAY KORAM
相关产品推荐
相关产品推荐

