[英]how to change panda data-frame values effectively
Say I have 2 dataframes.假设我有 2 个数据框。
first :首先:
100K rows
columns: ID, ch1,ch2...ch10,
binary values
second第二
1000K rows
columns: ID, CH.
say I wanna add to DF2 the values of DF1, in 1 column that it will fit the ID&chan.说我想将 DF1 的值添加到 DF2,在 1 列中它适合 ID&chan。
I tried to do it with a simple for-loop but it took a few minutes just to get past the first 10K rows.. I wonder how it can be done effectively - as if I've done it with numpy, it'd work faster.我试图用一个简单的 for 循环来做它,但花了几分钟才通过前 10K 行..我想知道如何有效地完成它 - 就像我用 numpy 完成它一样快点。 just to clarify, the IDs can be shuffled in DF2.. so I can't predict it's location based on DF1.只是为了澄清,ID 可以在 DF2 中混洗 .. 所以我无法根据 DF1 预测它的位置。
thanks in advance!提前致谢!
Updated post:更新的帖子:
Ah, okay.啊好吧。 I think I get what you're saying.我想我明白你在说什么。 Maybe try something like this out:也许尝试这样的事情:
df1 = pd.DataFrame({ "ID":[1,2,3], "ch1":[0,-1,0], "ch2":[0,0,0], "ch3":[-1,0,1] })
new_df = pd.DataFrame()
min_ch = 1
max_ch = 3
for i in range(min_ch,max_ch+1):
this_ch_str = "ch"+str(i)
temp_df = df1[["ID",this_ch_str]].copy()
temp_df["CH"] = i
temp_df = temp_df.rename(columns={this_ch_str:"val"})
temp_df = temp_df[["CH","ID","val"]]
new_df = new_df.append(temp_df)
It's a bit convoluted, but it gets the job done.这有点令人费解,但它完成了工作。
Original post:原帖:
I think the "merge" function might be what you're looking for.我认为“合并”功能可能是您正在寻找的。
Here's a quick example:这是一个快速示例:
import pandas as pd
df1 = pd.DataFrame({"ID":[1,2,3,4,5],"col1":["A","B","C","D","E"]})
df2 = pd.DataFrame({"ID":[1,2,3,4,5,6,7,8,9,10],"col2":["x","y","z","x","y","z","x","y","z","x"]})
df3 = pd.merge(left=df1,right=df2,on="ID",how="left")
The result stored in df3 will look like this:存储在 df3 中的结果将如下所示:
ID col1 col2
0 1 A x
1 2 B y
2 3 C z
3 4 D x
4 5 E y
Hope that's what you were looking for!希望这就是你要找的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.