简体   繁体   English

在 Pandas 中移动值的最佳方法是什么?

[英]What is the best way to move values around in Pandas?

I'm trying to move some values around.我正在尝试改变一些价值观。 My idea is that I'm trying to create a function where you send a name into it and it prints out all your matchups from the csv files.我的想法是,我正在尝试创建一个 function,您可以在其中发送一个名称,它会打印出 csv 文件中的所有匹配。 That's working so far but what I really want is the aforementioned name in one column and their opponents in another.到目前为止这是有效的,但我真正想要的是在一栏中提到上述名称,在另一栏中显示他们的对手。

Here's what I have so far but I can't seem to figure out where I'm going wrong.到目前为止,这是我所拥有的,但我似乎无法弄清楚我哪里出错了。

def get_matchups_by_name(name):
    df = PD_MATCHUPS
    df = df[(df['Team 1 Name'] == name) | (df['Team 2 Name'] == name)] # Getting rows which include the correct name
    if df['Team 1 Name'].str.contains(name).any():
        df['Team'] = df['Team 1 Name']
        df['Opponent'] = df['Team 2 Name']
        df['Score'] = df['Team 1 Pts']
        df['Opp Score'] = df['Team 2 Pts']
    elif df['Team 2 Name'].str.contains(name).any():
        df['Team'] = df['Team 2 Name']
        df['Opponent'] = df['Team 1 Name']
        df['Score'] = df['Team 2 Pts']
        df['Opp Score'] = df['Team 1 Pts']
    return df[['Week', 'Team', 'Score', 'Opp Score', 'Opponent']]

print(get_matchups_by_name('Dagur'))

Printing this spits out:打印出来:

    Week      Team   Score  Opp Score  Opponent
4      1     Dagur  120.65     105.40    Knútur
9      2       Óli  140.65     155.00     Dagur
16     3     Dagur  103.15      95.75   Brynjar
18     4     Björn   96.00     110.60     Dagur
28     5     Dagur   92.25     154.70      Aron

Here's what I want it to look like for everyone, using another name for this example:这是我希望每个人看起来像的样子,在此示例中使用另一个名称:

print(get_matchups_by_name('Björn'))
    Week   Team   Score  Opp Score  Opponent
0      1  Björn   86.95      76.80      Tóti
6      2  Björn  150.25     106.25      Aron
12     3  Björn  129.65      63.80  Eyjólfur
18     4  Björn   96.00     110.60     Dagur
24     5  Björn   99.10     138.05   Brynjar
...
94    17  Björn  136.60     125.80      Tóti

Finally just to show what the data looks like originally:最后只是为了展示数据最初的样子:

    Week Team 1 Name  Team 1 Pts  Team 2 Pts Team 2 Name   Diff
0      1       Björn       86.95       76.80        Tóti  10.15
1      1      Steini      155.95      157.45    Haraldur   1.50
2      1         Óli      113.50      102.55     Brynjar  10.95
3      1    Eyjólfur      110.05      119.95        Aron   9.90
4      1       Dagur      120.65      105.40      Knútur  15.25

I made it work with this:我让它与这个一起工作:

def get_matchups_by_name(name):
    df = PD_MATCHUPS
    df = df[(df['Team 1 Name'] == name) | (df['Team 2 Name'] == name)] # Filtering out rows which only include the name
    df.loc[df['Team 1 Name'] == name, 'Team'] = name
    df.loc[df['Team 1 Name'] == name, 'Score'] = df['Team 1 Pts']
    df.loc[df['Team 2 Name'] == name, 'Team'] = name
    df.loc[df['Team 2 Name'] == name, 'Score'] = df['Team 2 Pts']
    df.loc[df['Team 1 Name'] != name, 'Opponent'] = df['Team 1 Name']
    df.loc[df['Team 1 Name'] != name, 'Opp Score'] = df['Team 1 Pts']
    df.loc[df['Team 2 Name'] != name, 'Opponent'] = df['Team 2 Name']
    df.loc[df['Team 2 Name'] != name, 'Opp Score'] = df['Team 2 Pts']
    return df[['Week', 'Team', 'Score', 'Opp Score', 'Opponent']]

Which just looks like a really convoluted and bad way to achieve it.这看起来像是一种非常复杂和糟糕的实现方式。 Also the terminal is just screaming at me with these warnings:此外,终端只是用这些警告对我尖叫:

/opt/homebrew/lib/python3.9/site-packages/pandas/core/indexing.py:1684: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self.obj[key] = infer_fill_value(value)
/opt/homebrew/lib/python3.9/site-packages/pandas/core/indexing.py:1817: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_column(loc, value, pi)
/opt/homebrew/lib/python3.9/site-packages/pandas/core/indexing.py:1681: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self.obj[key] = empty_value
/opt/homebrew/lib/python3.9/site-packages/pandas/core/indexing.py:1773: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_column(ilocs[0], value, pi)

If anyone finds a better way to do this, please let me know.如果有人找到更好的方法来做到这一点,请告诉我。

You can use np.where here.你可以在这里使用np.where I tried with df.where , but it didn't work very well in this instance (from what I tried at least), so the slightly more confusing np.where it was!我尝试使用df.where ,但在这种情况下效果不佳(至少从我的尝试来看),所以np.where稍微更令人困惑!

The np.where is as follows: np.where如下:

  • If Team 1 Name is name (True/False)...如果Team 1 Namename (True/False)...
  • Then return week, team 1 name, team 1 pts, team 2 pts, team 2 name然后返回week, team 1 name, team 1 pts, team 2 pts, team 2 name
  • Else return week, team 2 name, team 2 pts, team 1 pts, team 1 name否则返回week, team 2 name, team 2 pts, team 1 pts, team 1 name
  • Returns an array返回一个数组

A dataframe is then created using this array, where the desired column names are given.然后使用此数组创建 dataframe,其中给出了所需的列名。

import pandas as pd
import numpy as np

PD_MATCHUPS = pd.read_csv("Data.csv", header=0)

def get_matchups_by_name(name):
    df = PD_MATCHUPS.copy()
    df = df[(df['Team 1 Name'] == name) | (df['Team 2 Name'] == name)]
    
    array = np.where(df["Team 1 Name"] == name,
                   [df["Week"], df["Team 1 Name"], df["Team 1 Pts"], df["Team 2 Pts"], df["Team 2 Name"]],
                   [df["Week"], df["Team 2 Name"], df["Team 2 Pts"], df["Team 1 Pts"], df["Team 1 Name"]])
    matches = pd.DataFrame({'Week': array[0], 'Team': array[1], 'Score': array[2],
                            'Opp Score': array[3], 'Opponent': array[4]})
    
    return matches

print(get_matchups_by_name('Björn'))

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

相关问题 对 Pandas 数据框中的所有值求和的最佳方法是什么? - What's the best way to sum all values in a Pandas dataframe? 在 pandas 中找到最接近日期时间值的最佳方法是什么? - What's best way to find closest values of datetime in pandas? 从 Pandas Dataframe 中删除唯一值的最佳方法是什么? - What is the best way to delete unique values from Pandas Dataframe? 用单独的Pan​​das系列中的值替换NaN值(在Pandas DataFrame中)的最佳方法是什么? - What's the best way to replace NaN values (in a Pandas DataFrame) with values from a separate Pandas Series? 从pandas数据帧中获取值时,除去每个值周围的['']最有说服力的方法是什么? - When taking values from a pandas dataframe what is the most eloquent way to remove the [''] around each value? 替换 pandas 中的列值的最佳方法? - Best way to replace column values in pandas? 删除pandas中列的最佳方法是什么 - What is the best way to remove columns in pandas 在熊猫数据框中计算回报的最佳方法是什么? - what is the best way to calculate the return in a pandas dataframe? 在pandas中对数据进行分组和排序的最佳方法是什么? - What is the best way to group and sort data in pandas? 在 Python pandas DataFrame 中交换值以清理数据的最佳方法是什么 - What is the best way to swap values in Python pandas DataFrame to clean up the data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM