0

I have 2 dataframes, c_r and x. I am trying to append an empty dataframe, with this code:

new_df = pd.DataFrame()

for i in c_r:
    for j in x:
        if c_r[i].dtype != object and x[j].dtype != object:
            if i == j:
                col_c = c_r[i]
                col_j = x[j]
                new_df[i+'-Diff'] = col_c - col_j
        
        else:
            break

But I keep getting back an empty data frame. Can anyone suggest what I am doing wrong? Thanks

5
  • What is c_r ? Commented Dec 3, 2020 at 13:48
  • 1
    Apologies, I edited my question to answer your question. Both are dataframes with usually the same columns Commented Dec 3, 2020 at 13:49
  • 1
    Apologies(again),but what is your expected output.When I run you code on two fictive df (c_r and x as two column df), I get new_df as the difference between them. Do this: Share a few rows of the two df and how you want the output to look like. Commented Dec 3, 2020 at 13:58
  • 1
    No need to apologise. Can you please move to this post: stackoverflow.com/questions/65109811/… ? And I can delete this one. The other post is the same question but with way more description! Commented Dec 3, 2020 at 13:59
  • 1
    stackoverflow.com/a/65127506/6361531 I believe you looping is unneccessary. Commented Dec 3, 2020 at 14:04

1 Answer 1

1

Your code works just fine, but might be unecessary. But if you want to use it. I created the dfs as c_r in

EOL - CL Per $;Access - CL Per $;Total Impact - CL Per $
-0.02;-0.39;-0.01
-0.02;-0.39;-0.02
-0.02;-0.39;-0.01
-0.02;-0.39;-0.02

and x in

EOL - CL Per $;Access - CL Per $;Total Impact - CL Per $
-0.02;-0.39;0.05
-0.02;-0.39;0.03
-0.02;-0.39;0.06
-0.02;-0.39;0.04

And then

c_r = pd.read_csv(r"C:/users/k_sego/c_r.csv", sep=";")
x = pd.read_csv(r"C:/users/k_sego/x.csv", sep=";")

Your code

new_df = pd.DataFrame()

for i in c_r:
    for j in x:
        if c_r[i].dtype != object and x[j].dtype != object:
            if i == j:
                col_c = c_r[i]
                col_j = x[j]
                new_df[i+'-Diff'] = col_c - col_j
        
        else:
            break

works just ok and gives

   EOL - CL Per $-Diff  Access - CL Per $-Diff  Total Impact - CL Per $-Diff
0                  0.0                     0.0                         -0.06
1                  0.0                     0.0                         -0.05
2                  0.0                     0.0                         -0.07
3                  0.0                     0.0                         -0.06

So,if it doesn'twork for you it must have to do with the files you have.

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

5 Comments

Thank you for this btw. This is very strange! Did you apply exactly my code from above? Also, can you please check what version of pandas to do you have?
pd.__version__ '1.0.5'. Have you checked the dtype of your data?
Mine is '0.25.3', I have a feeling that might play a role?
Haha wise words. I will do that now. Thanks for your time.
Thank you. Unfortunately I get the same problem. It doesn't work well. It gives the whole result in the 1st row, at index 0....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.