操作后将熊猫数据帧保存在循环中

时间:2019-12-18 03:03:53

标签: python pandas dataframe

我有一个循环,该循环采用一系列现有的数据帧并操纵其格式和值。我需要知道如何在循环结束时创建包含已修改内容的新数据框。

示例如下:

import pandas as pd

# Create datasets
First = {'GDP':[200,175,150,100]}
Second = {'GDP':[550,200,235,50]}

# Create old_dataframes
old_df_1 = pd.DataFrame(First)
old_df_2 = pd.DataFrame(Second)

# Define references and dictionary
old_dfs = [old_df_1, old_df_2]
new_dfs = ['new_df_1','new_df_2']
dictionary = {}

# Begin Loop
for df, name in zip(old_dfs, new_dfs):

    # Multiply all GDP values by 1.5 in both dataframes
    df = df * 1.5    

    # ISSUE HERE - Supposed to Create new data frames 'new_df_1' & 'new_df_2' containing df*1.5 values: Only appends to dictionary. Does not create new_df_1 & new_df_2
    dictionary[name] = df

# Check for the existance of 'new_df_1 & new_df_2' (They will not appear)
%who_ls DataFrame

问题:我已经在上面标记了问题。我的代码未创建“ new_df_1”和“ new_df_2”数据框。它只是将它们附加到字典中。我需要能够将new_df_1和new_df_2 创建为单独的数据框。

2 个答案:

答案 0 :(得分:0)

from copy import deepcopy   #  to copy old dataframes appropriately

# create 2 lists, first holds old dataframes and second holds modified ones
old_dfs_list, new_dfs_list = [pd.DataFrame(First), pd.DataFrame(Second)], []

# process old dfs one by one by iterating over old_dfs_list, 
# copy, modify each and append it to list of new_dfs_list with same index as 
# old df ... so old_dfs_list[1] is mapped to new_dfs_list[1]

for i in range(len(old_dfs_list)):
  # a deep copy prevent changing old dfs by reference
  df_deep_copy = deepcopy(old_dfs_list[i]) 
  df_deep_copy['GDP'] *= 1.5
  new_dfs_list.append(df_deep_copy)

print( old_dfs_list[0] )   # to check that old dfs are not changed
print( new_dfs_list[0] )

results_before_after

您也可以尝试使用字典而不是列表来使用您喜欢的名称:

import pandas as pd
datadicts_dict = { 
                    'first' :{'GDP':[200,175,150,100]}, 
                    'second':{'GDP':[550,200,235,50]}, 
                    'third' :{'GDP':[600,400,520,100, 800]}
                    }

# Create datasets and store it in a python dictionary
old_dfs_dict, new_dfs_dict = {}, {}    # initialize 2 dicts to hold original and modified dataframes

# process datasets one by one by iterating over datadicts_dict, 
# convert to df save it in old_dfs_dict with same name as the key
# copy, modify each and put it in new_dfs_dict with same key 
# so dataset of key 'first' in datadicts_dict is saved as old_dfs_dict['first'] 
# modified and mapped to new_dfs_dict['first']

for dataset_name, data_dict in datadicts_dict.items():
    old_dfs_dict[dataset_name] = pd.DataFrame({'GDP':data_dict['GDP']})
    new_dfs_dict[dataset_name] = pd.DataFrame({'GDP':data_dict['GDP']}) * 1.5

print( old_dfs_dict['third'] )   # to check that old dfs are not changed
print( new_dfs_dict['third'] )

答案 1 :(得分:0)

最后,我通过思考以上答案,终于找到了可行的解决方案。我面临的问题是-从字典内部提取附加数据。我真的没有想到我可以从循环的外部字典中提取数据,然后形成数据框。

.
.
.
 # Begin Loop
    for df, name in zip(old_dfs, new_dfs):
    # Multiply all GDP values by 1.5 in both dataframes
    df = df * 1.5    

    # ISSUE HERE - Supposed to Create new data frames 'new_df_1' & 'new_df_2' containing df*1.5 values: Only appends to dictionary. Does not create new_df_1 & new_df_2
    dictionary[name] = df

# Solution - Extract from Dictionary and form Dataframe
new_df_1 = pd.DataFrame.from_dict(dictionary['new_df_1'])
new_df_2 = pd.DataFrame.from_dict(dictionary['new_df_2'])

# Check for the existance of 'new_df_1 & new_df_2'
%who_ls DataFrame