#!/usr/bin/env python # coding: utf-8 # # Table of Contents #

1  将数据进行初步整理
1.1  Year 2007
1.2  Year 2008
1.3  Year 2009
1.4  Year 2010
1.5  Year 2011
1.6  Year 2012
1.7  Year 2013
1.7.1  Data Source 1
1.7.2  Data Source 2 补充数据
1.7.3  Data Source 3: 2013年使用excel数据源
1.8  Year 2014
1.9  Year 2015
1.10  Year 2016
1.11  Year 2017
2  数据合并
# In[ ]: import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns get_ipython().run_line_magic('', 'matplotlib inline') # # 将数据进行初步整理 # ## Year 2007 # * 2007年的数据,原始数据的单位为十亿美元 # In[205]: df_2007 = pd.read_csv('./data/data_forbes_2007.csv', encoding='gbk', thousands=',') print('the shape of DataFrame: ', df_2007.shape) print(df_2007.dtypes) df_2007.head(3) # * 更新columns的命名 # In[206]: column_update = ['Year', 'Rank', 'Company_cn_en', 'Country_cn_en', 'Industry_cn', 'Sales', 'Profits', 'Assets', 'Market_value'] df_2007.columns = column_update df_2007.head(3) # * **通过前面的分析可看出,只有“Market_value”是数字类型,找出'Sales','Profits'及'Assets'中非数字的内容** # In[207]: df_2007[df_2007['Sales'].str.contains('.*[A-Za-z]', regex=True)] # * 用replace()方法替换“Sales”列中含有字母的内容 # In[208]: df_2007['Sales'] = df_2007['Sales'].replace('([A-Za-z])', '', regex=True) # * 查看替换后的结果 # In[209]: df_2007.loc[[117,616,880], :] # * **查看“Assets”列中非数字的内容** # In[210]: df_2007[df_2007['Assets'].str.contains('.*[A-Za-z]', regex=True)] # * 替换非数字的内容,以及替换千分位间隔符号 # In[211]: # 将数字后面的字母进行替换 df_2007['Assets'] = df_2007['Assets'].replace('([A-Za-z])', '', regex=True) # 千分位数字的逗号被识别为string了,需要替换 df_2007['Assets'] = df_2007['Assets'].replace(',', '', regex=True) df_2007.loc[616, :] # * **发现“Profits”中有NaN值,需要先进行替换** # In[212]: df_2007[pd.isnull(df_2007['Profits'])] # * 将NaN值填充为 0 # In[213]: df_2007['Profits'].fillna(0, inplace=True) df_2007.loc[[958,1440,1544,1912], :] # * 将“Profits”列中非数字的内容进行替换,并查看替换后的结果 # In[214]: df_2007['Profits'] = df_2007['Profits'].replace('([A-Za-z])', '', regex=True) df_2007.loc[[117,616,880], :] # * **将sting类型的数字转换为数据类型,这里使用 pd.to_numeric() 方法** # In[215]: df_2007['Sales'] = pd.to_numeric(df_2007['Sales']) df_2007['Profits'] = pd.to_numeric(df_2007['Profits']) df_2007['Assets'] = pd.to_numeric(df_2007['Assets']) df_2007.dtypes # * **拆分"Company_cn_en"列**,新生成两列,分别为公司英文名称和中文名称 # In[216]: df_2007['Company_en'],df_2007['Company_cn'] = df_2007['Company_cn_en'].str.split('/', 1).str print(df_2007['Company_en'][:5]) print(df_2007['Company_cn'] [-5:]) # In[217]: df_2007.tail(3) # * **拆分"Country_cn_en"列**,新生成两列,分别为国家中文名称和英文名称 # In[218]: df_2007['Country_cn'],df_2007['Country_en'] = df_2007['Country_cn_en'].str.split('(', 1).str print(df_2007['Country_cn'][:5]) print(df_2007['Country_en'][-5:]) # * 由于国家的英文名称中,最后有半个括号,需要去除,用 Series.str.slice()方法 # * 参数表示选取从开始到倒数第二个,即不要括号")" # In[219]: df_2007['Country_en'] = df_2007['Country_en'].str.slice(0,-1) df_2007.head(3) # * 考虑的中国的企业有区分为中国大陆,中国香港,中国台湾 # * 对应的国家英文名称也需要修改下 # * 中国大陆:CN;中国香港:CN-HK;中国台湾:CN-TA # In[220]: df_2007[df_2007['Country_cn'].str.contains('中国',regex=True)] # In[221]: df_2007['Country_en'] = df_2007['Country_en'].replace(['HK.*','TA'],['CN-HK', 'CN-TA'],regex=True) df_2007[df_2007['Country_en'].str.contains('CN',regex=True)] # * 考虑到其他年份,公司所在行业有用英文名称展示的,这里添加一列英文的行业名称,但内容是空白 # In[222]: df_2007['Industry_en'] = '' df_2007.tail(5) # * **将列名进行重新排序** # In[223]: columns_sort = ['Year', 'Rank', 'Company_cn_en','Company_en', 'Company_cn', 'Country_cn_en', 'Country_cn', 'Country_en', 'Industry_cn', 'Industry_en', 'Sales', 'Profits', 'Assets', 'Market_value'] # In[224]: # 按指定list重新将columns进行排序 df_2007 = df_2007.reindex(columns=columns_sort) print(df_2007.shape) print(df_2007.dtypes) df_2007.head(3) # ## Year 2008 # * 数据加载 # In[225]: df_2008 = pd.read_csv('./data/data_forbes_2008.csv', encoding='gbk', thousands=',') print('the shape of DataFrame: ', df_2008.shape) print(df_2008.dtypes) df_2008.head() # * 更新columns的名称 # In[226]: df_2008.columns = ['Year', 'Rank', 'Company_en', 'Company_cn','Country_en', 'Industry_en', 'Sales', 'Profits', 'Assets', 'Market_value'] df_2008.head() # * 添加空白列,使之与其他年份的格式保持一致 # In[227]: df_2008['Company_cn_en'], df_2008['Country_cn_en'], df_2008['Country_cn'], df_2008['Industry_cn'] = ['','','',''] df_2008.head() # In[228]: col_digit = ['Sales', 'Profits', 'Assets', 'Market_value'] for col in col_digit: # 将数字后面的字母进行替换 df_2008[col] = df_2008[col].replace('([A-Za-z])', '', regex=True) # 千分位数字的逗号被识别为string了,需要替换 df_2008[col] = df_2008[col].replace(',', '', regex=True) #将数字型字符串转换为可进行计算的数据类型 df_2008[col] = pd.to_numeric(df_2008[col]) # In[229]: # df_2008['Sales'] = pd.to_numeric(df_2008['Sales']) # df_2008['Profits'] = pd.to_numeric(df_2008['Profits']) # df_2008['Assets'] = pd.to_numeric(df_2008['Assets']) # * 按指定list重新将columns进行排序 # In[230]: # 按指定list重新将columns进行排序 df_2008 = df_2008.reindex(columns=columns_sort) print(df_2008.shape) print(df_2008.dtypes) df_2008.head() # ## Year 2009 # * 数据加载 # In[231]: df_2009 = pd.read_csv('./data/data_forbes_2009.csv', encoding='gbk') print('the shape of DataFrame: ', df_2009.shape) df_2009.head() # * 更新columns名称 # In[232]: df_2009.columns = ['Year', 'Rank', 'Company_cn_en', 'Country_en', 'Industry_en', 'Sales', 'Profits', 'Assets', 'Market_value'] df_2009.head() # * **拆分"Company_cn_en"列**,新生成两列,分别为公司英文名称和中文名称 # In[233]: df_2009['Company_en'],df_2009['Company_cn'] = df_2009['Company_cn_en'].str.split('/', 1).str print(df_2009['Company_en'][:5]) print(df_2009['Company_cn'] [-5:]) df_2009.head() # * 添加空白列 # In[234]: df_2009['Country_cn_en'], df_2009['Country_cn'], df_2009['Industry_cn'] = ['','',''] df_2009.head() # In[235]: col_digit = ['Sales', 'Profits', 'Assets', 'Market_value'] for col in col_digit: # 将数字后面的字母进行替换 df_2009[col] = df_2009[col].replace('([A-Za-z])', '', regex=True) # 千分位数字的逗号被识别为string了,需要替换 df_2009[col] = df_2009[col].replace(',', '', regex=True) df_2009[col] = pd.to_numeric(df_2009[col]) # * 将columns重新排序 # In[236]: # 按指定list重新将columns进行排序 df_2009 = df_2009.reindex(columns=columns_sort) print(df_2009.shape) print(df_2009.dtypes) df_2009.head() # ## Year 2010 # * 数据加载,单位为十亿美元 # In[238]: df_2010 = pd.read_csv('./data/data_forbes_2010.csv', encoding='gbk', header=None) print('the shape of DataFrame: ', df_2010.shape) df_2010.head() # * 添加columns的名称 # In[239]: df_2010.columns = ['Year', 'Rank', 'Company_cn','Company_en', 'Country_en', 'Industry_en', 'Sales', 'Profits', 'Assets', 'Market_value'] df_2010.head() # * 添加空白列 # In[240]: df_2010['Company_cn_en'], df_2010['Country_cn_en'], df_2010['Country_cn'], df_2010['Industry_cn'] = ['','','',''] df_2010.head() # * 1600行的标题重复,需要删除 # In[241]: df_2010 = df_2010.drop(1600) # df_2010.drop(1600, inplace=True) # In[242]: col_digit = ['Sales', 'Profits', 'Assets', 'Market_value', 'Rank'] for col in col_digit: # 将数字后面的字母进行替换 df_2010[col] = df_2010[col].replace('([A-Za-z])', '', regex=True) # 千分位数字的逗号被识别为string了,需要替换 df_2010[col] = df_2010[col].replace(',', '', regex=True) df_2010[col] = pd.to_numeric(df_2010[col]) # * 将columns重新排序 # In[243]: # 按指定list重新将columns进行排序 df_2010 = df_2010.reindex(columns=columns_sort) print(df_2010.shape) print(df_2010.dtypes) df_2010.head() # In[244]: # df_2010.to_csv('data_forbes_2010_update.csv') # ## Year 2011 # * 加载数据 # * 原始数据的单位为 亿美元,需要统一成十亿美元 # In[246]: df_2011 = pd.read_csv('./data/data_forbes_2011.csv', encoding='gbk', header=None, thousands=',') print('the shape of DataFrame: ', df_2011.shape) df_2011.head() # * 添加colunmns名称 # In[247]: df_2011.columns = ['Year', 'Rank', 'Company_en','Company_cn', 'Country_cn', 'Industry_cn', 'Sales', 'Profits', 'Assets', 'Market_value'] df_2011.head() # In[248]: df_2011.dtypes # * 修改货币单位为 十亿美元 # In[249]: df_2011[['Sales','Profits','Assets','Market_value']] =df_2011[['Sales','Profits','Assets','Market_value']].apply(lambda x: x/10) df_2011.head() # * 添加空白列 # In[250]: df_2011['Company_cn_en'], df_2011['Country_cn_en'], df_2011['Country_en'], df_2011['Industry_en'] = ['','','',''] df_2011.head() # * 按指定list重新将columns进行排序 # In[251]: # 按指定list重新将columns进行排序 df_2011 = df_2011.reindex(columns=columns_sort) print(df_2011.shape) print(df_2011.dtypes) df_2011.head() # ## Year 2012 # * 采集的原始数据有异常,用Pandas不能顺利加载,需要进行手动调整; # * 2012年原始数据的单位是 亿美元 # In[252]: df_2012 = pd.read_csv('./data/data_forbes_2012.csv', encoding='gbk',header=None) print('the shape of DataFrame: ', df_2012.shape) print(df_2012.dtypes) df_2012.head() # * 数据初步整理 # In[253]: # 更新列名 df_2012.columns = ['Year', 'Rank', 'Company_cn','Company_cn_en', 'Country_cn', 'Sales', 'Profits', 'Assets', 'Market_value'] # 拆分"Company_cn_en"列,新生成两列,分别为公司英文名称和中文名称 df_2012['Company_cn'],df_2012['Company_en'] = df_2012['Company_cn_en'].str.split('/', 1).str # print(df_2012['Company_cn'][:5]) # print(df_2012['Company_en'] [-5:]) # 将数据单位转换成十亿美元 df_2012[['Sales','Profits','Assets','Market_value']] =df_2012[['Sales','Profits','Assets','Market_value']].apply(lambda x: x/10) # 添加空白列 df_2012['Country_en'],df_2012['Country_cn_en'], df_2012['Industry_cn'], df_2012['Industry_en'] = ['','','',''] # 按指定list重新将columns进行排序 df_2012 = df_2012.reindex(columns=columns_sort) print(df_2012.shape) print(df_2012.dtypes) df_2012.head() # ## Year 2013 # ### Data Source 1 # * 2013年原始数据的单位是 亿美元 # In[254]: df_2013 = pd.read_csv('./data/data_forbes_2013.csv', encoding='gbk',header=None) print('the shape of DataFrame: ', df_2013.shape) print(df_2013.dtypes) df_2013.head() # * 2013年的数据只有1991条记录,数据可能有缺失,待进一步核实。 # In[ ]: # ### Data Source 2 补充数据 # * 2013年的数据在网上继续寻找,发现Economy Watch网站有相关数据,于是进行数据爬取 # * Economy Watch:Forbes Global 2000: China's Largest Companies # * http://www.economywatch.com/companies/forbes-list/china.html # In[255]: df_2013_economy = pd.read_csv('./data/data_forbes_2013_economywatch.csv', encoding='gbk') print('the shape of DataFrame: ', df_2013_economy.shape) print(df_2013_economy.dtypes) df_2013_economy.head() # * 发现数据只有1984条记录,也缺少相关记录 # * 继续寻找其他记录 # ### Data Source 3: 2013年使用excel数据源 # * 后来,在找到一个excel文件,发现数据记录是完整的,如下: # In[256]: df_2013_all = pd.read_excel('./data/data_forbes_2013_all.xlsx') print('the shape of DataFrame: ', df_2013_all.shape) print(df_2013_all.dtypes) df_2013_all.head() # * 数据整理 # In[257]: # 更新列名 df_2013_all.columns = ['Rank', 'Company_cn_en', 'Country_cn', 'Sales', 'Profits', 'Assets', 'Market_value'] # 拆分"Company_cn_en"列,新生成两列,分别为公司英文名称和中文名称 df_2013_all['Company_cn'],df_2013_all['Company_en'] = df_2013_all['Company_cn_en'].str.split('/', 1).str # print(df_2013_all['Company_cn'][:5]) # print(df_2013_all['Company_en'] [-5:]) # 将数据单位转换成十亿美元 df_2013_all[['Sales','Profits','Assets','Market_value']] =df_2013_all[['Sales','Profits','Assets','Market_value']].apply(lambda x: x/10) # 添加年份2013 df_2013_all['Year'] = 2013 # 添加空白列 df_2013_all['Country_en'],df_2013_all['Country_cn_en'], df_2013_all['Industry_cn'], df_2013_all['Industry_en'] = ['','','',''] df_2013_all['Rank'] = pd.to_numeric(df_2013_all['Rank']) # 按指定list重新将columns进行排序 df_2013_all = df_2013_all.reindex(columns=columns_sort) print(df_2013_all.shape) print(df_2013_all.dtypes) df_2013_all.head() # In[ ]: # ## Year 2014 # * 采集的原始数据有异常,用Pandas不能顺利加载,需要进行手动调整; # * 2014年原始数据的单位是 亿美元 # In[ ]: # columns_sort = ['Year', 'Rank', 'Company_cn_en','Company_en','Company_cn', 'Country_cn_en', 'Country_cn', 'Country_en', 'Industry_cn', 'Industry_en','Sales', 'Profits', 'Assets', 'Market_value'] # In[258]: df_2014 = pd.read_csv('./data/data_forbes_2014.csv', encoding='gbk',header=None) print('the shape of DataFrame: ', df_2014.shape) print(df_2014.dtypes) df_2014.head() # In[259]: # 更新列名 df_2014.columns = ['Year', 'Rank', 'Company_cn','Company_cn_en', 'Country_cn', 'Sales', 'Profits', 'Assets', 'Market_value'] # 拆分"Company_cn_en"列,新生成两列,分别为公司英文名称和中文名称 df_2014['Company_cn'],df_2014['Company_en'] = df_2014['Company_cn_en'].str.split('/', 1).str # print(df_2014['Company_cn'][:5]) # print(df_2014['Company_en'] [-5:]) # 将数据单位转换成十亿美元 df_2014[['Sales','Profits','Assets','Market_value']] =df_2014[['Sales','Profits','Assets','Market_value']].apply(lambda x: x/10) # 添加空白列 df_2014['Country_en'],df_2014['Country_cn_en'], df_2014['Industry_cn'], df_2014['Industry_en'] = ['','','',''] # 按指定list重新将columns进行排序 df_2014 = df_2014.reindex(columns=columns_sort) print(df_2014.shape) df_2014.head() # ## Year 2015 # * 2015年原始数据的单位是 亿美元 # * 2015年,有部分企业是重复的 # In[260]: df_2015 = pd.read_csv('./data/data_forbes_2015.csv', encoding='gbk',header=None) print('the shape of DataFrame: ', df_2015.shape) print(df_2015.dtypes) df_2015.head() # * 整理数据 # In[261]: # 更新列名 df_2015.columns = ['Year', 'Rank', 'Company_cn','Company_cn_en', 'Country_cn', 'Sales', 'Profits', 'Assets', 'Market_value'] # 拆分"Company_cn_en"列,新生成两列,分别为公司英文名称和中文名称 df_2015['Company_cn'],df_2015['Company_en'] = df_2015['Company_cn_en'].str.split('/', 1).str # print(df_2014['Company_cn'][:5]) # print(df_2014['Company_en'] [-5:]) # 将数据单位转换成十亿美元 df_2015[['Sales','Profits','Assets','Market_value']] =df_2015[['Sales','Profits','Assets','Market_value']].apply(lambda x: x/10) # 添加空白列 df_2015['Country_en'],df_2015['Country_cn_en'], df_2015['Industry_cn'], df_2015['Industry_en'] = ['','','',''] # 按指定list重新将columns进行排序 df_2015 = df_2015.reindex(columns=columns_sort) print(df_2015.shape) df_2015.head() # * 数据有2020行,有重复行,需要去除重复行 # In[262]: # 数据有2020行,有重复行,需要去除重复行 # inplace=True,使去重生效 df_2015.drop_duplicates('Company_cn_en', inplace=True) # * 查看'Company_cn_en'是否还有重复行 # In[263]: # 查看'Company_cn_en'是否还有重复行 df_2015[df_2015['Company_cn_en'].duplicated()] # * 查看数据行数 # In[264]: print('the shape of DataFrame: ', df_2015.shape) # print(df_2015.dtypes) # df_2015.head() # ## Year 2016 # * 2016年原始数据的单位,各个单元格不一样 # * 通过查看原始数据,发现有些行的数据可能不准确 # * 对比网站的数据,进行了手动修改 # * http://www.askci.com/news/finance/20160530/13364822511_7.shtml # * 修改的情况如下: # In[265]: df_2016 = pd.read_csv('./data/data_forbes_2016.csv', encoding='gbk',header=None) print('the shape of DataFrame: ', df_2016.shape) # 更新列名 df_2016.columns = ['Year', 'Rank', 'Company_cn','Company_en', 'Country_en', 'Sales', 'Profits', 'Assets', 'Market_value'] print(df_2016.dtypes) df_2016.head() # * 编写自定义处理函数 # In[266]: def pro_col(df, col): # 替换相关字符串,如有更多的替换情形,可以自行添加 df[col] = df[col].str.replace('$','') df[col] = df[col].str.replace('^[A-Za-z]+$','') df[col] = df[col].str.replace('B','') # 注意这里是'-$',即以'-'结尾,而不是'-',因为有负数 df[col] = df[col].str.replace('-$','') df[col] = df[col].str.replace(',','') # 处理含有百万“M”为单位的数据,即以“M”结尾的数据 # 思路: # (1)设定查找条件mask; # (2)替换字符串“M”为空值 # (3)用pd.to_numeric()转换为数字 # (4)除以1000,转换为十亿美元,与其他行的数据一致 mask = df[col].str.endswith('M') df.loc[mask, col] = pd.to_numeric(df.loc[mask, col].str.replace('M',''))/1000 # 将字符型的数字转换为数字类型 df[col] = pd.to_numeric(df[col]) return df # * 应用自定义函数进行数据处理 # In[267]: # pro_col(df_2016, 'Sales') # pro_col(df_2016, 'Profits') # pro_col(df_2016, 'Assets') # pro_col(df_2016, 'Market_value') cols = ['Sales', 'Profits', 'Assets', 'Market_value'] for col in cols: pro_col(df_2016, col) print('the shape of DataFrame: ', df_2016.shape) print(df_2016.dtypes) df_2016.head() # * 添加空白列 # In[268]: # 添加空白列 df_2016['Company_cn_en'],df_2016['Country_cn_en'], df_2016['Country_cn'], df_2016['Industry_cn'], df_2016['Industry_en'] = ['','','','',''] # 按指定list重新将columns进行排序 df_2016 = df_2016.reindex(columns=columns_sort) print(df_2016.shape) df_2016.head() # ## Year 2017 # * 2017年原始数据的单位,各个单元格不一样 # In[269]: df_2017 = pd.read_csv('./data/data_forbes_2017.csv', encoding='gbk') # 更新列名 df_2017.columns = ['Year', 'Rank', 'Company_cn','Company_en', 'Country_en', 'Sales', 'Profits', 'Assets', 'Market_value'] print('the shape of DataFrame: ', df_2017.shape) df_2017.head() # In[270]: cols = ['Sales', 'Profits', 'Assets', 'Market_value'] for col in cols: pro_col(df_2017, col) print('the shape of DataFrame: ', df_2017.shape) print(df_2017.dtypes) df_2017.head() # In[271]: # 添加空白列 df_2017['Company_cn_en'],df_2017['Country_cn_en'], df_2017['Country_cn'], df_2017['Industry_cn'], df_2017['Industry_en'] = ['','','','',''] # 按指定list重新将columns进行排序 # columns_sort = ['Year', 'Rank', 'Company_cn_en','Company_en', # 'Company_cn', 'Country_cn_en', 'Country_cn', # 'Country_en', 'Industry_cn', 'Industry_en', # 'Sales', 'Profits', 'Assets', 'Market_value'] df_2017 = df_2017.reindex(columns=columns_sort) print(df_2017.shape) df_2017.head() # In[ ]: # df_2017.to_csv('data_forbes_2017_update.csv') # # 数据合并 # In[272]: df_concat = pd.concat([df_2007, df_2008, df_2009, df_2010, df_2011, df_2012, df_2013_all, df_2014, df_2015, df_2016, df_2017], ignore_index=True) df_concat # In[ ]: # df_concat.to_csv('data_forbes_concat.csv') # In[273]: print('the shape of DataFrame: ', df_concat.shape) print(df_concat.dtypes) # * 若 "Country_en"列有空值,则用"Conutry_cn"列的值替换 # In[274]: df_concat.loc[df_concat['Country_en']=='', 'Country_en'] = df_concat.loc[df_concat['Country_en']=='', 'Country_cn'] # In[275]: # df_concat[df_concat['Year']==2011] # In[276]: # df_concat.to_csv('data_forbes_concat-1.csv') # * 将国家名称统一成英文名称,这里主要用的是 replace()方法 # In[277]: list_origin = [']','AR', 'AS','AU', 'US', 'BE', 'BR', 'BS', 'BU', 'CA', 'CH', 'CI', 'CN-HK', 'CN-TA', 'CO', 'CZ', 'DE', 'EG', 'FI', 'FR', 'GE', 'GR', 'Hong Kong', 'Hong Kong/China', 'HU', 'IC', 'ID', 'IN', 'IR', 'IS','IT', 'JA', 'JO', 'KO','LI', 'LU', 'MX', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PK', 'PL', 'PO', 'RU', 'SI', 'SP', 'SU', 'SW', 'SZ', 'Taiwan', 'TH', 'TU', 'UK', 'VE', '阿根廷', '阿联酋', '阿曼', '埃及', '爱尔兰', '奥地利', '澳大利亚', '巴基斯坦', '巴林', '巴拿马', '巴西', '百慕大', '比利时', '波多黎各', '波兰', '丹麦', '德国', '多哥', '俄罗斯', '法国', '菲律宾', '芬兰', '哥伦比亚', '哈萨克斯坦', '海峡群岛', '韩国', '荷兰', '加拿大', '捷克共和国', '卡塔尔', '开曼群岛', '科威特', '克罗地亚', '黎巴嫩', '利比里亚', '列支敦士登', '列支敦斯登', '卢森堡', '马来西亚', '毛里求斯', '美国', '秘鲁', '摩洛哥', '墨西哥', '南非', '尼日利亚', '挪威', '葡萄牙', '日本', '瑞典', '瑞士', '塞浦路斯', '沙特$', '沙特阿拉伯', '斯洛伐克', '泰国', '土耳其', '委内瑞拉', '西班牙', '希腊', '新加坡', '新西兰', '匈牙利', '以色列', '意大利', '印度$', '印度尼西亚', '印尼', '英国', '约旦', '越南', '智利', '中国大陆', '中国台湾', '中国香港', 'CN$', '中国$', 'Netherlands'] list_replace = ['','Argentina', 'Austria','Australia', 'United States', 'Belgium', 'Brazil', 'Bahamas', 'Bermuda', 'Canada', 'Chile', 'Cayman Islands', 'China-HongKong', 'China-Taiwan', 'Colombia', 'Czech Republic', 'Denmark', 'Egypt', 'Finland', 'France', 'Germany', 'Greece', 'China-HongKong', 'China-HongKong', 'Hungary', 'Iceland', 'Indonesia', 'India', 'Ireland', 'Israel', 'Italy', 'Japan', 'Jordan', 'South Korea','Liberia', 'Luxembourg', 'Mexico', 'Netherland', 'Norway', 'New Zealand', 'Panama', 'Peru', 'Philippines', 'Pakistan', 'Poland', 'Portugal', 'Russia', 'Singapore', 'Spain', 'Saudi Arabia', 'Sweden', 'Switzerland', 'China-Taiwan', 'Thailand', 'Turkey', 'United Kingdom', 'Venezuela', 'Argentina', 'United Arab Emirates', 'Oman', 'Egypt', 'Ireland', 'Austria', 'Australia', 'Pakistan', 'Bahrain', 'Panama', 'Brazil', 'Bermuda', 'Belgium', 'Puerto Rico', 'Poland', 'Denmark', 'Germany', 'Togo', 'Russia', 'France', 'Philippines', 'Finland', 'Colombia', 'Kazakhstan', 'Channel Islands', 'South Korea', 'Netherland', 'Canada', 'Czech Republic', 'Qatar', 'Cayman Islands', 'Kuwait', 'Croatia', 'Lebanon', 'Liberia', 'Liechtenstein', 'Liechtenstein', 'Luxembourg', 'Malaysia', 'Mauritius', 'United States', 'Peru', 'Morocco', 'Mexico', 'South Africa', 'Nigeria', 'Norway', 'Portugal', 'Japan', 'Sweden', 'Switzerland', 'Cyprus', 'Saudi Arabia', 'Saudi Arabia', 'Slovakia', 'Thailand', 'Turkey', 'Venezuela', 'Spain', 'Greece', 'Singapore', 'New Zealand', 'Hungary', 'Israel', 'Italy', 'India', 'Indonesia', 'Indonesia', 'United Kingdom', 'Jordan', 'Vietnam', 'Chile', 'China', 'China-Taiwan', 'China-HongKong', 'China', 'China', 'Netherland'] df_concat['Country_en'] = df_concat['Country_en'].replace(list_origin,list_replace, regex=True) # df_concat['Country_en'] = df_concat['Country_en'].replace(['CN','中国'],['China', 'China'], regex=True) # df_concat.head() # In[ ]: # df_concat.to_csv('data_forbes_concat-2.csv') # * 进一步替换,包括一家公司属于多个国家的情况 # In[278]: # 进一步替换,包括一家公司属于多个国家的情况 # 注意这里没有 regex=True df_concat['Country_en'] = df_concat['Country_en'].replace(['China-China-Taiwan',r'China-HongKong/China', r'Australia)/United Kingdom(United Kingdom', r'Netherland)/United Kingdom(United Kingdom', r'Panama)/United Kingdom(United Kingdom', r'SA)/United Kingdom(United Kingdom', r'United Kingdom)/Australia(SA', r'United Kingdom)/Netherland(Netherland', r'United Kingdom)/South Africa(SA' , 'Netherland/United Kingdom', 'Panama/United Kingdom', 'Australia/United Kingdom'], ['China-Taiwan', 'China-HongKong', 'United Kingdom/Australia', 'United Kingdom/Netherland', 'United Kingdom/Panama', 'United Kingdom/Australia', 'United Kingdom/Australia', 'United Kingdom/Netherland', 'United Kingdom/South Africa', 'United Kingdom/Netherland', 'United Kingdom/Panama', 'United Kingdom/Australia']) # df_concat.head() # In[279]: # df_concat.to_csv('data_forbes_concat-3.csv') # * 有两个特殊情况,同样的英文缩写,代表的国家可能不同 # * 需要进一步处理 # * SA可以代表Australia or South Africa # * MA可以代表 Malaysia or Morocco # * 如下图 # 处理这类特殊情况的思路与步骤为: # # (a)将此类英文缩写用空白值替换, # # (b)将空白值替换为当前样本的中文国家名称 # # (c)将中文国家名称替换为各自的英文国家名称 # In[280]: # 注意需要以 "$"结尾 df_concat['Country_en'] = df_concat['Country_en'].replace(['MA$','SA$'],['', ''], regex=True) # df_concat[df_concat['Country_en']==''] # * 若 "Country_en"列有空值,则用"Conntry_cn"列的值替换 # In[281]: df_concat.loc[df_concat['Country_en']=='', 'Country_en'] = df_concat.loc[df_concat['Country_en']=='', 'Country_cn'] # In[ ]: # df_concat.to_csv('data_forbes_concat-4.csv') # In[282]: df_concat['Country_en'] = df_concat['Country_en'].replace(['澳大利亚','马来西亚', '摩洛哥', '南非'], ['Australia', 'Malaysia', 'Morocco', 'South Africa']) # In[ ]: df_concat.to_csv('data_forbes_concat.csv') # In[ ]: