Python:如何从熊猫的特定列中获取价值?

时间:2020-05-06 23:11:12

标签: pandas

How the dataset looks

我正在使用Kaggle(https://www.kaggle.com/ash316/learn-pandas-with-pokemons)中的数据集。我希望能够绘制出评分最高的口袋妖怪(按TOTAL排名)的攻击与防御的关系图。我希望此图具有标有神奇宝贝名称的点。但是,我不知道如何返回与TOTAL关联的NAME列的值。例如,我想要780之类的东西同时返回Mega Rayquaza和Mega MewtwoY。

将数据集格式化为此的代码是:

import pandas as pd   #importing all the important packages
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')

df =  pd.read_csv('../input/Pokemon.csv')  #read the csv file and save it into a variable

df.columns = df.columns.str.upper().str.replace('_', '') #change into upper case

df = df.set_index('NAME') #change and set the index to the name attribute

df.index = df.index.str.replace(".*(?=Mega)", "")

df=df.drop(['#'],axis=1) #drop the columns with axis=1;axis=0 is for rows

df['TYPE 2'].fillna(df['TYPE 1'], inplace=True) #fill NaN values in Type2 with corresponding values of Type

strong=df.sort_values(by='TOTAL', ascending=False) #sorting the rows in descending order
strong.drop_duplicates(subset=['TYPE 1'],keep='first') #since the rows are now sorted in descending oredr
#thus we take the first row for every new type of pokemon i.e the table will check TYPE 1 of every pokemon
#The first pokemon of that type is the strongest for that type
#so we just keep the first row

很抱歉,这是否令人困惑或曾经被问过,我似乎找不到合适的方式表达我的问题(我确信这是非常基本的)。同样,如果代码很奇怪,您可以在上面的链接中找到它。

0 个答案:

没有答案