添加具有值翻轉的重復Pandas行

[英]Adding duplicate Pandas rows that have values flipped


I know - confusing title. But hear me out.

我知道 - 令人困惑的頭銜。但是聽我說。

If I have a pandas DF as such:

如果我有這樣的熊貓DF:

  date       team opponent
0  20141028      magic  hornets
1  20141028  mavericks    spurs
2  20141029      76ers   pacers

And I'd like to create a DF off it where the rows are duplicated but 'team' and 'opponent' values are flipflopped, how would I best do that in pandas?

而且我想在它上面創建一個DF,其中行是重復的,但是'團隊'和'對手'的值是翻轉的,我怎么能在熊貓中做到最好?

So i would want this:

所以我想要這個:

  date       team opponent
0  20141028      magic  hornets
1  20141028    hornets    magic
2  20141028  mavericks    spurs
3  20141028      spurs mavericks
4  20141029      76ers   pacers
5  20141029     pacers   76ers

thanks!

謝謝!

3 个解决方案

#1


4  

You can create another dataframe called df1 with flipped columns, and then append it to df.

您可以使用翻轉列創建另一個名為df1的數據框,然后將其附加到df。

df1 = df.copy()
df1.columns = ['date', 'opponent', 'team']
df.append(df1).sort_index().reset_index(drop=True)

       date   opponent       team
0  20141028    hornets      magic
1  20141028      magic    hornets
2  20141028      spurs  mavericks
3  20141028  mavericks      spurs
4  20141029     pacers      76ers
5  20141029      76ers     pacers

#2


2  

This isn't all that elegant, but it gets the job done:

這不是那么優雅,但它完成了工作:

In [1]: import pandas as pd

In [4]: df = pd.read_clipboard()

In [6]: df
Out[6]:
       date       team opponent
0  20141028      magic  hornets
1  20141028  mavericks    spurs
2  20141029      76ers   pacers

Use loc for creating a new frame:

使用loc創建新框架:

In [7]: df2 = df.loc[df.index,['date','team','opponent']]

Then swap the axis titles:

然后交換軸標題:

In [8]: df2.columns= ['date','opponent','team']

In [9]: df2
Out[9]:
       date   opponent     team
0  20141028      magic  hornets
1  20141028  mavericks    spurs
2  20141029      76ers   pacers

Last, use pd.concat() to merge the two frames:

最后,使用pd.concat()合並兩個框架:

In [10]: pd.concat([df, df2])
Out[10]:
       date   opponent       team
0  20141028    hornets      magic
1  20141028      spurs  mavericks
2  20141029     pacers      76ers
0  20141028      magic    hornets
1  20141028  mavericks      spurs
2  20141029      76ers     pacers

#3


1  

You can rearrange the columns and then concatenate.

您可以重新排列列然后連接。

df_new = pd.concat([df, pd.DataFrame(df[['date', 'opponent', 'team']].values,
                                     columns=['date', 'team', 'opponent'])])
>>> df_new 
       date       team   opponent
0  20141028      magic    hornets
1  20141028  mavericks      spurs
2  20141029      76ers     pacers
0  20141028    hornets      magic
1  20141028      spurs  mavericks
2  20141029     pacers      76ers

If you really need the ordering you specified (original pair followed by reversed pair), you can sort it as suggested by @ Jianxun Li:

如果你真的需要你指定的訂單(原始配對后跟反轉配對),你可以按照@ Jianxun Li的建議對其進行排序:

df_new.sort_index().reset_index(drop=True)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/08/25/7207731e95196a81a2162e00fd3d0712.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com