练习一、现有一份关于美剧《权力的游戏》剧本的数据集,请解决以下问题:
在所有的数据中,一共出现了多少人物?
564个人物
方法一:直接统计
import pandas as pd
df = pd.read_csv(
r'D:\Desktop\pandas\Learning\joyful-pandas-master\data\Game_of_Thrones_Script.csv')
result = df['Name'].nunique()
print(df)
print(result)
输出为:
564
方法二:根据表长统计:
import pandas as pd
df = pd.read_csv(
r'D:\Desktop\pandas\Learning\joyful-pandas-master\data\Game_of_Thrones_Script.csv')
result = df['Name'].value_counts()
print(df)
print(result)
输出为:
tyrion lannister 1760
jon snow 1133
daenerys targaryen 1048
cersei lannister 1005
jaime lannister 945
...
robett glover 1
tribesmen of vale 1
bolton bannerman 1
watchman 1
officer 1
Name: Name, Length: 564, dtype: int64
2.以单元格计数(即简单把一个单元格视作一句),谁说了最多的话?
import pandas as pd
df = pd.read_csv(
r'D:\Desktop\pandas\Learning\joyful-pandas-master\data\Game_of_Thrones_Script.csv')
result = df['Name'].value_counts()
man = result.index[0]
print(df)
print(result)
print(man)
输出结果:
tyrion lannister 1760
jon snow 1133
daenerys targaryen 1048
cersei lannister 1005
jaime lannister 945
...
lord bolton 1
officer 1
eddision 1
pyattpree 1
rodrik cassal 1
Name: Name, Length: 564, dtype: int64
tyrion lannister
3.以单词计数,谁说了最多的单词?
import pandas as pd
df = pd.read_csv(
r'D:\Desktop\pandas\Learning\joyful-pandas-master\data\Game_of_Thrones_Script.csv')
print(df)
result1 = df.groupby('Sentence')['Name'].unique().apply(lambda x: x[0]).value_counts()
result1 = result1.index[0]
print(result1)
结果为:tyrion lannister
练习二、现有一份关于科比的投篮数据集,请解决如下问题:
1.哪种action_type和combined_shot_type的组合是最多的?
import pandas as pd
df = pd.read_csv(
r'D:\Desktop\pandas\Learning\joyful-pandas-master\data\Kobe_data.csv')
print(df)
result1 = pd.Series(df['action_type']).value_counts()
result1 = result1.index[0]
print(result1)
结果为:Jump Shot
2. 在所有被记录的game_id中,遭遇到最多的opponent是一个支?
import pandas as pd
df = pd.read_csv(
r'D:\Desktop\pandas\Learning\joyful-pandas-master\data\Kobe_data.csv')
print(df)
result1 = df.groupby('game_id')['opponent'].unique().apply(lambda x: x[0]).value_counts()
result1 = result1.index[0]
print(result1)
结果为:SAS