Here I am trying to concat dataframe A and B with C using a for loop.
data = [['Alex',10],['Bob',12],['Clarke',13]]
A = pd.Dataframe(data, columns=['Name','Age'])
B = pd.Dataframe(data, columns=['Name','Age'])
C = pd.Dataframe(data, columns=['Name','Age'])
A.columns ---> Index(['Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age'], dtype='object')
C.columns ---> Index(['Name', 'Age'], dtype='object')
for df in [A, B]:
df = pd.concat([df, C], axis=1)
A.columns ---> Index(['Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age'], dtype='object')
df.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
Why its not concatenating C with original A, B dataframes. Why it is creating a new df Dataframe?
I want after for loop:
A.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
B.columns ---> Index(['Name', 'Age', 'Name', 'Age'], dtype='object')
You're using Python mappings of names to object differently than how they work (you might be confused by other languages' references).
When you use
for df in [A, B]:
df = pd.concat([df, C], axis=1)
Then df on the right side means "the object mapped to by the name df" (that is, A then B). df on the left side is just the name df. Your loop is therefore not modifying the original objects at all.
You can use
A, B = pd.concat([A, C], axis=1), pd.concat([B, C], axis=1)
If you really must use a loop, you can use a dict. First place the object there,
dfs = {'A': A, 'B': B}
then refer to them only through the dict:
for k, v in dfs.items():
dfs[k] = pd.concat([v, C], axis=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With