Taylor
Taylor

Reputation: 23

Creating Empty Data Frame and adding Rows to it

I'm trying to create an empty data frame with pandas that has two columns (['originating_number','terminating_number']. After I create this new empty data frame, my goal is to loop through another data frame I have an add a row to this new empty data frame if certain criteria is met. Unfortunately, I'm running into a snag with either initializing empty data frame or adding to it. Here is my current code (where records represents my other data frame that I'm working with):

    verified_frame = pd.DataFrame(columns=['originating_number_2', 'terminating_number_2'])

    for index, row in records.iterrows():
        originating_number_length = len(str(row['originating_number']))
        terminating_number_length = len(str(row['terminating_number']))

        if originating_number_length == 10 and terminating_number_length == 10:

            temp_df = pd.DataFrame([row['originating_number'],row['terminating_number']])
            verified_frame.append(temp_df)

However, when I set a trace after this code (within the if block), I can see that my temp_df has the right values, but when I look at the verified_frame, the values have not been added.

Thanks for the help!

Upvotes: 2

Views: 1922

Answers (2)

panktijk
panktijk

Reputation: 1614

If you only need to filter out rows from the original frame based on a condition, you can do it directly instead of iterating through each row:

verified_frame  = records[(records['originating_number'] == 10) & (records['terminating_number'] == 10)]['originating_number', 'terminating_number']
verified_frame.columns = ['originating_number_2', 'terminating_number_2']

Iterating through dataframe rows is not very efficient and should always be considered as the last option.

Upvotes: 1

Ian Thompson
Ian Thompson

Reputation: 3285

I think you are almost there, you just need to assign the new verfied_frame to its appended self. Try this:

verified_frame = pd.DataFrame(columns=['originating_number_2', 'terminating_number_2'])

for index, row in records.iterrows():
    originating_number_length = len(str(row['originating_number']))
    terminating_number_length = len(str(row['terminating_number']))

    if originating_number_length == 10 and terminating_number_length == 10:

        temp_df = pd.DataFrame([row['originating_number'],row['terminating_number']])

# reassign verfied_frame here
        verified_frame = verified_frame.append(temp_df)

Here is the documentation supporting this https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html

Upvotes: 3

Related Questions