I have Python Data Frame like below:
df = pd.DataFrame({"ID1" : [1,2,3], "ID2" : [44,55,66]})
Then I create loop like below:
s_list = list()
t_list = list()
for s in df["ID1"].values:
for t in df["ID2"].values:
s_list.append(s)
t_list.append(t)
result = pd.DataFrame()
result["res1"] = s_list
result["res2"] = t_list
result
Neverheless result of this loop is like below:
And I need to repare this loop so as to acheive result like this (so identical combination ID1 x ID2 like in df):
Try the pandas .to_list()
method instead of a using loop.
df = pd.DataFrame({"ID1" : [1,2,3], "ID2" : [44,55,66]})
s_list = df["ID1"].to_list()
t_list = df["ID2"].to_list()
The result is the following:
s_list = [1, 2, 3]
t_list = [44, 55, 66]
Both s_list
and t_list
are now lists. Test it out by print(type(your_list))
which results in <class 'list'>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.