Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas:在两个不同的列中获取具有相同值对的两个不同行_Python_Python 3.x_Pandas - Fatal编程技术网

Python Pandas:在两个不同的列中获取具有相同值对的两个不同行

Python Pandas:在两个不同的列中获取具有相同值对的两个不同行,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有两列_Id和_ParentId以及这个示例数据。我想用这个将_Id与_ParentId分组 _Id _ParentId 1 NaN 2 NaN 3 1.0 4 2.0 5 NaN 6 2.0 分组后,结果应如下所示 _Id _ParentId 1 Na

我有两列_Id和_ParentId以及这个示例数据。我想用这个将_Id与_ParentId分组

       _Id  _ParentId
        1        NaN
        2        NaN
        3        1.0
        4        2.0
        5        NaN
        6        2.0
分组后,结果应如下所示

       _Id  _ParentId
        1        NaN
        3        1.0
        2        NaN
        4        2.0
        6        2.0
        5        NaN
其主要目的是将_Id所属的组与_ParentId所属的组(例如_id3所属的_id1)


我尝试使用groupby和duplicated,但似乎无法获得上面显示的结果。

temp上使用
sort\u值

In [3188]: (df.assign(temp=df._ParentId.combine_first(df._Id))
              .sort_values(by='temp').drop('temp', 1))
Out[3188]:
   _Id  _ParentId
0    1        NaN
2    3        1.0
1    2        NaN
3    4        2.0
5    6        2.0
4    5        NaN
细节

In [3189]: df._ParentId.combine_first(df._Id)
Out[3189]:
0    1.0
1    2.0
2    1.0
3    2.0
4    5.0
5    2.0
Name: _ParentId, dtype: float64

In [3190]: df.assign(temp=df._ParentId.combine_first(df._Id))
Out[3190]:
   _Id  _ParentId  temp
0    1        NaN   1.0
1    2        NaN   2.0
2    3        1.0   1.0
3    4        2.0   2.0
4    5        NaN   5.0
5    6        2.0   2.0

temp

In [3188]: (df.assign(temp=df._ParentId.combine_first(df._Id))
              .sort_values(by='temp').drop('temp', 1))
Out[3188]:
   _Id  _ParentId
0    1        NaN
2    3        1.0
1    2        NaN
3    4        2.0
5    6        2.0
4    5        NaN
细节

In [3189]: df._ParentId.combine_first(df._Id)
Out[3189]:
0    1.0
1    2.0
2    1.0
3    2.0
4    5.0
5    2.0
Name: _ParentId, dtype: float64

In [3190]: df.assign(temp=df._ParentId.combine_first(df._Id))
Out[3190]:
   _Id  _ParentId  temp
0    1        NaN   1.0
1    2        NaN   2.0
2    3        1.0   1.0
3    4        2.0   2.0
4    5        NaN   5.0
5    6        2.0   2.0

您的预期输出与输入完全相同,只是IDs 4和IDs 6在一起,而NAN位于不同的位置。不可能有那样的预期产出

下面是group by理想的工作方式:

print("Original: ")
print(df)

df = df.fillna(-1) # if not replaced with another character , the grouping won't show NaNs. 
df2 = df.groupby('_Parent')

print("\nAfter grouping: ")
for key, item in df2:
    print (df2.get_group(key))
输出:

Original: 
   _Id  _Parent
0    1      NaN
1    2      NaN
2    3      1.0
3    4      2.0
4    5      NaN
5    6      2.0

After grouping: 
   _Id  _Parent
0    1      0.0
1    2      0.0
4    5      0.0
   _Id  _Parent
2    3      1.0
   _Id  _Parent
3    4      2.0
5    6      2.0

您的预期输出与输入完全相同,只是IDs 4和IDs 6在一起,而NAN位于不同的位置。不可能有那样的预期产出

下面是group by理想的工作方式:

print("Original: ")
print(df)

df = df.fillna(-1) # if not replaced with another character , the grouping won't show NaNs. 
df2 = df.groupby('_Parent')

print("\nAfter grouping: ")
for key, item in df2:
    print (df2.get_group(key))
输出:

Original: 
   _Id  _Parent
0    1      NaN
1    2      NaN
2    3      1.0
3    4      2.0
4    5      NaN
5    6      2.0

After grouping: 
   _Id  _Parent
0    1      0.0
1    2      0.0
4    5      0.0
   _Id  _Parent
2    3      1.0
   _Id  _Parent
3    4      2.0
5    6      2.0

我想你说得对!你已经为自己赢得了一枚读心术徽章。这非常有效。在对值进行排序之前,我从未想过将它们合并到预处理中。谢谢我想你说得对!你已经为自己赢得了一枚读心术徽章。这非常有效。在对值进行排序之前,我从未想过将它们合并到预处理中。谢谢