python DataCamp:Python数据科学工具箱(第2部分)https://www.datacamp.com/courses/python-data-science-toolbox-part

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python DataCamp:Python数据科学工具箱(第2部分)https://www.datacamp.com/courses/python-data-science-toolbox-part相关的知识,希望对你有一定的参考价值。

# Define plot_pop()
def plot_pop(filename, country_code):

    # Initialize reader object: urb_pop_reader
    urb_pop_reader = pd.read_csv(filename, chunksize=1000)

    # Initialize empty DataFrame: data
    data = pd.DataFrame()
    
    # Iterate over each DataFrame chunk
    for df_urb_pop in urb_pop_reader:
        # Check out specific country: df_pop_ceb
        df_pop_ceb = df_urb_pop[df_urb_pop['CountryCode'] == country_code]

        # Zip DataFrame columns of interest: pops
        pops = zip(df_pop_ceb['Total Population'],
                    df_pop_ceb['Urban population (% of total)'])

        # Turn zip object into list: pops_list
        pops_list = list(pops)

        # Use list comprehension to create new DataFrame column 'Total Urban Population'
        df_pop_ceb['Total Urban Population'] = [int(tup[0] * tup[1]) for tup in pops_list]
    
        # Append DataFrame chunk to data: data
        data = data.append(df_pop_ceb)

    # Plot urban population data
    data.plot(kind='scatter', x='Year', y='Total Urban Population')
    plt.show()

# Set the filename: fn
fn = 'ind_pop_data.csv'

# Call plot_pop for country code 'CEB'
plot_pop('ind_pop_data.csv', 'CEB')

# Call plot_pop for country code 'ARB'
plot_pop('ind_pop_data.csv', 'ARB')

以上是关于python DataCamp:Python数据科学工具箱(第2部分)https://www.datacamp.com/courses/python-data-science-toolbox-part的主要内容,如果未能解决你的问题,请参考以下文章

python DataCamp:用于数据科学的中级Python https://www.datacamp.com/courses/intermediate-python-for-data-scienc

python DataCamp:Python数据科学工具箱(第2部分)https://www.datacamp.com/courses/python-data-science-toolbox-part

python DataCamp:用Python导入数据(第1部分)

学习笔记之Intermediate Python for Data Science | DataCamp

数据科学中R VS Python:获胜者是...

数据科学中R VS Python:获胜者是...