2

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!

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, by reassigning the verified_frame, I can see in my set_trace() that that temp_df is being added to the verified_frame. However, it's adding the data incorrectly into a whole new column instead of the original two columns, so that's my next mission to figure out. Thanks for the help!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.