Python - 使用数据框的值来指定新数据帧的名称

时间:2018-06-09 10:43:57

标签: python pandas

我有一个名为' population'的数据框。看起来像这样 -

Index   Name      Pop
0      Mumbai    100000
1      Delhi    233299

...

我想创建名称为' Mumbai',#34;德里' ....的数据框。 所以我编码了这个 -

npop = len(population)   #number of popuation centres

    for i in np.arange(0,npop):
        population[i]["Name"] = pd.DataFrame(index = pd.date_range('2016-01-01 00:00:00','2017-01-01 00:00:00', freq ='H').tolist(),columns = ['Susceptible','Exposed','Infected','Removed'])  #Create a blank dataframe
        population[i]['Name'][0]['Susceptible'] = population.iloc[i][0]  #Sets Current population to Susceptible

正如您所看到的,我要做的是创建一个包含4列的空白数据框,其索引是给定的日期时间范围。然后,我想将该城市的pop值设置为新数据框中的Susceptible value列的第1行单元格。

1 个答案:

答案 0 :(得分:0)

我认为需要通过列dictionary of DataFrame的{​​{3}}循环填充每个DataFrame,最后得到dfs = {} for i, x in df.groupby('Name'): dfs[i] = pd.DataFrame({'Susceptible': x['Pop'].iloc[0]}, index = pd.date_range('2016-01-01 00:00:00','2017-01-01 00:00:00', freq ='H'), columns = ['Susceptible','Exposed','Infected','Removed']) #print (dfs) print (dfs['Mumbai']) s:

dfs = {}
for i, x in df.groupby('Name'):
    dfs[i] = pd.DataFrame(index = pd.date_range('2016-01-01 00:00:00','2017-01-01 00:00:00', freq ='H'),
                columns = ['Susceptible','Exposed','Infected','Removed'])  #Create a blank dataframe
    dfs[i].loc[dfs[i].index[0], 'Susceptible'] = x['Pop'].iloc[0]

print (dfs['Mumbai'].head())
                   Susceptible Exposed Infected Removed
2016-01-01 00:00:00      100000     NaN      NaN     NaN
2016-01-01 01:00:00         NaN     NaN      NaN     NaN
2016-01-01 02:00:00         NaN     NaN      NaN     NaN
2016-01-01 03:00:00         NaN     NaN      NaN     NaN
2016-01-01 04:00:00         NaN     NaN      NaN     NaN

编辑:

如果需要仅创建第一个值:

user