zoukankan      html  css  js  c++  java
  • 构建Dataframe格式的数据

    构建Dataframe格式的数据
    数据集中的数据:
    intereset-rates.csv

    populations.csv

    # Load the data into Python lists
    
    with open('../data/countries/interest-rates.csv', 'r') as f:
        int_rates_col_names = next(f).strip().split(';')
        int_rates = [line.split(';') for line in f.read().splitlines()]
        
    with open('../data/countries/populations.csv', 'r') as f:
        populations_col_names = next(f).strip().split(';')
        populations = [line.split(';') for line in f.read().splitlines()]
    
    import pandas as pd
    df_int_rates= pd.DataFrame(int_rates, columns=int_rates_col_names)
    df_populations=pd.DataFrame(populations, columns=populations_col_names)
    pd.options.diaplay.max_rows=10000
    
    #更改数据的类型
    df_int_rates['']=df_int_rates[''].astype(float, copy=False)
    df_int_rates['']=pd.to_datetime(df_int_rates[''])
    
    #合并两个DataFrame数据集
    df_merge=pd.merge(
    				df_populations,
    				df_int_rates,
    				#按照两个表中的哪两列进行合并
    				left_on='Country(or dependency)'
    				right_on='Country or currency union',
    				#连接的方式,类似于数据库
    				how='outer'
    				)
    

    #更改数据集中某一个位置的值
    #比如讲U.S.更改为United States
    col='Country(or dependency)'
    #对于下一句代码的理解:判断col这一列所有元素是否等于'U.S.',等于就是true,否则是false, 返回的是行号和真假值
    mask= df.populations[col]=='U.S.'
    df_populations.loc[mask, col]='United States'
    
    #更改列的名字
    del df_merge['Country or currency union']
    name_map = {'Country (or dependency)': 'Country',
                'Population (2018)': 'Population',
                'Central bank interest rate (%)': 'Interest rate'}
    df_merge=df_merge.rename(columns=name_map)
    

  • 相关阅读:
    OpenGL ES学习001---绘制三角形
    Mac关机时处于黑屏状态
    静态变量和实例变量的区别(配图解释专业术语,通俗易懂)
    用shape画内圆外方,形成一个圆形头像
    最全的敏捷认证对比(CSM/PMI-ACP/EXIN)
    Certified Scrum Master CSM 中文资料大全
    如何打造优秀的远程敏捷团队(9步)
    博客搬家
    权威的国际敏捷认证Certified Scrum Master (CSM)
    博客园入驻了
  • 原文地址:https://www.cnblogs.com/share-sjb/p/10003971.html
Copyright © 2011-2022 走看看