I need to create a pandas dataframe using information from two different sources. For example,
JavaScript
x
4
1
for row in df.itertuples():
2
c1, c2, c3 = row.c1, row.c2, row.c3
3
returnedDict = function(row.c1, row.c2, row.c3)
4
The first 3 columns in the dataframe I want should contain c1, c2, c3
, and the rest of the columns come from the key of the returnedDict
. The number of keys in the returnedDict
is 100. How can I initialize such Dataframe and append the row in the dataframe at each for loop
?
Expected output
JavaScript
1
4
1
col1 col2 col3 key1 key100
2
--------------------------------------------------------------
3
c1 c2 c3 returnedDict[key1] returnedDict[key100]
4
Advertisement
Answer
If my understanding of your question is correct, here is an example of how to do it (Python >= 3.9.0):
JavaScript
1
8
1
import pandas as pd
2
3
# Toy dataframe
4
df = pd.DataFrame({"c1": [9, 4, 3], "c2": [2, 7, 1], "c3": [4, 6, 5]})
5
6
def function(c1, c2, c3):
7
return {"key1": c1 * 10, "key2": c2 * 4, "key3": c3 * 2}
8
JavaScript
1
8
1
dfs = []
2
for row in df.itertuples():
3
c1, c2, c3 = row.c1, row.c2, row.c3
4
returnedDict = function(row.c1, row.c2, row.c3)
5
dfs.append(pd.DataFrame({"col1": [c1], "col2": [c2], "col3": [c3]} | returnedDict))
6
7
df = pd.concat(dfs).reset_index(drop=True)
8
JavaScript
1
7
1
print(df)
2
# Output
3
col1 col2 col3 key1 key2 key3
4
0 9 2 4 90 8 8
5
1 4 7 6 40 28 12
6
2 3 1 5 30 4 10
7