Warm tip: This article is reproduced from stackoverflow.com, please click
dataframe pandas python indexing

Create mutliple dataframes by changing one column with a for loop?

发布于 2020-03-27 10:23:04

I am calculating heat decay from spent fuel rods using variable cooling times. How can I create multiple dataframes, by varying the cooling time column with a for loop, then write them to a file?

Using datetime objects, I am creating multiple columns of cooling time values by subtracting a future date from the date the fuel rod was discharged.

I then tried to use a for loop to index these columns into a new dataframe, with the intent to streamline multiple files by using newly created dataframes in a new function.

df = pd.read_excel('data')
df.columns = ['ID','Enr','Dis','Mtu']

# Discharge Dates
_0 = dt.datetime(2020,12,1)
_1 = dt.datetime(2021,6,1)
_2 = dt.datetime(2021,12,1)
_3 = dt.datetime(2022,6,1)

# Variable Cooling Time Columns
df['Ct_0[Years]'] = df['Dis'].apply(lambda x: (((_0 - x).days)/365))
df['Ct_1[Years]'] = df['Dis'].apply(lambda x: (((_1 - x).days)/365))
df['Ct_2[Years]'] = df['Dis'].apply(lambda x: (((_2 - x).days)/365))
df['Ct_3[Years]'] = df['Dis'].apply(lambda x: (((_3 - x).days)/365))

# Attempting to index columns into new data frame
for i in range(4):
    df = df[['ID','Mtu','Enr','Ct_%i[Years]'%i]]
    tfile = open('Inventory_FA_%s.prn'%i,'w')
    ### Apply conditions for flagging
    tfile.close()

I was expecting the created cooling time columns to be indexed into the newly defined dataframe df. Instead I received the following error;

KeyError: "['Ct_1[Years]'] not in index"

Thank you for the help.

Questioner
StupidPanda
Viewed
75
Engineero 2019-07-03 22:45

You are overwriting your dataframe in each iteration of your loop with the line:

df = df[['ID','Mtu','Enr','Ct_%i[Years]'%i]]

which is why you are fine on your first iteration (error doesn't say anything about 'Ct_0[Years]' not being in the index), and then die on your second iteration. You've dropped everything but the columns you selected in your first iteration. Select your columns into a temporary df instead:

for i in range(4):
    df_temp = df[['ID','Mtu','Enr','Ct_%i[Years]'%i]]
    tfile = open('Inventory_FA_%s.prn'%i,'w')
    ### Apply conditions for flagging using df_temp
    tfile.close()

Depending on what your conditions are, there might be a better way to do this that doesn't require making a temporary view into the dataframe, but this should help.