I’m new to pandas so forgive me if this is an easy question. I would like to create a dataframe by using elements from two different dataframes. What is the best way to do this? It looks like I will have to use some kind of nested for loop:
for a in df1.itertuples()
for b in df2.itertuples()
if df1.vega(a) = df2.vega(b)
delta = df2.gamma(a) – df1.gamma(b)
if delta > 0 or delta <0
zelda = df2.blabla(b)
EDIT: both dataframes have the same column names:
df1 = pd.DataFrame({'vega': [bla1, bla2, bla3, bla4], 'gamma': [242340.1, 466530.2, 325346, 34654365]})
df2 = pd.DataFrame({'vega': [bla6, bla2, bla7, bla4], 'gamma': [3454334, 896530.2, 32543, 34654]})
EDIT2: In this example the new dataframe would be:
zelda|
----- |
430000 |
-34619711 |
EDIT3: Sorry guys, your answers opened my eyes and made me rethink my problem as it seems no for loop at all is needed here. I added a ‘ranking’ column to my dataframes, shuffled the ‘vega’ column in df2 and added the ‘rho’ column to the resulting dataframe. (I actually have more columns in the dataframes, but they are all used in the same way as either ‘gamma’, ‘blabla’, or ‘ranking’ columns.)
Is this still possible using the merge solutions you guys came up with? Again, my apologies for the first incomplete question.
zelda = df2.blabla(b)
rho = df1.ranking(a)
df1 = pd.DataFrame({'vega': [bla1, bla2, bla3, bla4], 'gamma': [242340.1, 466530.2, 325346, 34654365, ‘blabla’: [regina, cactus, galileo, viking], ‘ranking’: [11, 34,65,46]]})
df2 = pd.DataFrame({'vega': [bla2, bla6, bla7, bla4], 'gamma': [3454334, 896530.2, 32543, 34654], ‘blabla’: [lucy, fletnix, ingrid, pablo], ‘ranking’: [45, 4,5,3]})
In this new example, the new dataframe would be:
delta| zelda| rho|vega
---------|-------|----|----
2987803,8|fletnix| 34|bla2
-34619711| pablo| 46|bla4