简体   繁体   English

熊猫:在数据框中重新分配值

[英]Pandas: Reassigning values in dataframe

Suppose I have two columns, ID1 and ID2 amongst many other columns in the dataframe. 假设我在数据帧的许多其他列中有两列,ID1和ID2。

ID1      |      ID2      |   etc.
123      |      345
876      |      114
etc.

I need to rearrange the values in ID1 and ID2 in such a way that ID1 always contains the lowest integer value. 我需要重新排列ID1和ID2中的值,使得ID1始终包含最小的整数值。 In this case, row 2 should be switched such that the dataframe looks like this: 在这种情况下,应该切换第2行,使得数据框看起来像这样:

ID1      |      ID2
123      |      345
114      |      876

I tried the following, which finds instances where ID2 < ID1 but I don't know how to assign the values in this case. 我尝试了以下,找到ID2 <ID1的实例,但我不知道在这种情况下如何分配值。

df.loc[df.ID2 < df.ID1, ['ID1','ID2']] = df.loc[df.ID2 < df.ID1, ['ID2','ID1']]

I need to do this in order to merge this dataframe with another dataframe on ID1 and ID2, where the values might not be aranged in the same way. 我需要这样做才能将此数据帧与ID1和ID2上的另一个数据帧合并,其中值可能不会以相同的方式排列。

Any suggestions? 有什么建议?

is that what you want? 那是你要的吗?

In [279]: df
Out[279]:
   ID1  ID2  ID3
0  123  345  100
1  876  114  200
2  111  222  300

In [280]: df[['ID1','ID2']] = df[['ID1','ID2']].apply(np.sort, axis=1)

In [281]: df
Out[281]:
   ID1  ID2  ID3
0  123  345  100
1  114  876  200
2  111  222  300

I guess the faster way would be: 我想更快的方法是:

df2 = df.copy()
mask = df.ID1 > df.ID2
df2.ix[mask, 'ID1'] = df.ix[mask, 'ID2']
df2.ix[mask, 'ID2'] = df.ix[mask, 'ID1']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM