0

I am concatenating a few dataframes together, however rather than rows just being appended into one large dataframe, they appear to be getting 'smushed' together. For example:

df1 = a | b
      1 | 2
      2 | 3
      3 | 4

df2 = a | b
      5 | 6
      7 | 8

df3 = pd.concat([df1, df2])

for index, row in df3.iterrows():
    print(df3.at[index, 'a'])
    
    # expected output:
        # 1
        # 2
        # 3
        # 5
        # 7

    # actual output: 
        # series containing [1, 5]
        # series containing [2, 7]
        # 3
        ...

If I export to csv, the data appears as expected, with one value per cell per row. But if I try to search for 1 in column 'a' in df3, it cannot find it because it is in a list instead of its own value in the cell. I have a workaround for this, but why is this happening in the first place?

A few things to note:

  • I have other projects where I do concats and they behave as expected, so it seems to have to do with the environment I'm working in.
  • I am on pandas 2.2.3
3
  • 1
    df3 = pd.concat([df1, df2], ignore_index=True) Commented Nov 14, 2024 at 20:57
  • What is df in df.at[index, 'a']? Commented Nov 14, 2024 at 20:58
  • sorry that should say df3 - thanks for catching that. This is just an example I wrote describing the issue, not my actual code Commented Nov 14, 2024 at 21:11

1 Answer 1

0

Thanks @BigBen - solution was forgetting to include 'ignore_index=True'. Now to figure out why my other projects worked without it...

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.