'loop over dictionary of pandas dataframes to find identical columns
I have a dictionary of pandas dataframes that have same column names. I want to loop over this dictionary and return columns with identical names with corresponding dataframe name (dictionary key).
I tried to do the following:
for first_dataframe in dct_dataframes.keys():
for second_dataframe in dct_dataframes.keys():
if first_dataframe != second_dataframe:
for column_first_dataframe in dct_dataframes[first_dataframe]:
for column_second_dataframe in dct_dataframes[second_dataframe]:
if column_first_dataframe == column_second_dataframe:
print(column_first_dataframe, dct_dataframes[first_dataframe][column_first_dataframe])
print(column_second_dataframe, dct_dataframes[second_dataframe][column_second_dataframe])
However, this does not give me correct output.
What should I correct in my loop and is there a way to to avoid this big loop? Thank you
Solution 1:[1]
If I understood your question, assuming your dictionary is something like this:
import pandas as pd
dct_dfs = {'df1': pd.DataFrame({'a':[1,2,3],'b':[4,5,6]}),
'df2': pd.DataFrame({'a':[7,8,9],'b':[10,11,12]})}
Then, I would proceed like this:
for col in dct_dfs['df1']:
for df in dct_dfs:
print(dct_dfs[df][col], '\n', df, '\n\n')
Output:
0 1
1 2
2 3
Name: a, dtype: int64
df1
0 7
1 8
2 9
Name: a, dtype: int64
df2
0 4
1 5
2 6
Name: b, dtype: int64
df1
0 10
1 11
2 12
Name: b, dtype: int64
df2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gioarma |
