Statement of Completion#6c7a36b0
Intro to Pandas for Data Analysis
easy
DataFrames practice: working with English Words
Resolution
Activities
Project.ipynb
In [3]:
import pandas as pd
In [6]:
df = pd.read_csv('words.csv', index_col='Word')
In [7]:
df.head()
Out[7]:
Char Count | Value | |
---|---|---|
Word | ||
aa | 2 | 2 |
aah | 3 | 10 |
aahed | 5 | 19 |
aahing | 6 | 40 |
aahs | 4 | 29 |
Activities¶
How many elements does this dataframe have?¶
In [8]:
df.size
Out[8]:
345642
What is the value of the word microspectrophotometries
?¶
In [19]:
df["Value"].max()
Out[19]:
319
What is the highest possible value of a word?¶
In [20]:
df.describe()
Out[20]:
Char Count | Value | |
---|---|---|
count | 172821.000000 | 172821.000000 |
mean | 9.087628 | 107.754179 |
std | 2.818285 | 39.317452 |
min | 2.000000 | 2.000000 |
25% | 7.000000 | 80.000000 |
50% | 9.000000 | 103.000000 |
75% | 11.000000 | 131.000000 |
max | 28.000000 | 319.000000 |
Which of the following words have a Char Count of 15
?¶
In [22]:
df[df["Char Count" == 15]]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: False The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[22], line 1 ----> 1 df[df["Char Count" == 15]] File /usr/local/lib/python3.11/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key) 4100 if self.columns.nlevels > 1: 4101 return self._getitem_multilevel(key) -> 4102 indexer = self.columns.get_loc(key) 4103 if is_integer(indexer): 4104 indexer = [indexer] File /usr/local/lib/python3.11/site-packages/pandas/core/indexes/base.py:3812, in Index.get_loc(self, key) 3807 if isinstance(casted_key, slice) or ( 3808 isinstance(casted_key, abc.Iterable) 3809 and any(isinstance(x, slice) for x in casted_key) 3810 ): 3811 raise InvalidIndexError(key) -> 3812 raise KeyError(key) from err 3813 except TypeError: 3814 # If we have a listlike key, _check_indexing_error will raise 3815 # InvalidIndexError. Otherwise we fall through and re-raise 3816 # the TypeError. 3817 self._check_indexing_error(key) KeyError: False
What is the highest possible length of a word?¶
In [23]:
df["Char Count"].max()
Out[23]:
28
What is the word with the value of 319
?¶
In [30]:
df.loc[df["Value"] == 319]
Out[30]:
Char Count | Value | |
---|---|---|
Word | ||
reinstitutionalizations | 23 | 319 |
What is the most common value?¶
In [38]:
df["Ratio"] = df["Value"]/df["Char Count"]
df["Ratio"]
Out[38]:
Word aa 1.000000 aah 3.333333 aahed 3.800000 aahing 6.666667 aahs 7.250000 ... zymotic 15.857143 zymurgies 15.888889 zymurgy 19.285714 zyzzyva 21.571429 zyzzyvas 21.250000 Name: Ratio, Length: 172821, dtype: float64
What is the shortest word with value 274
?¶
In [ ]:
Create a column Ratio
which represents the 'Value Ratio' of a word¶
In [ ]:
What is the maximum value of Ratio
?¶
In [40]:
df["Ratio"].max()
Out[40]:
22.5
What word is the one with the highest Ratio
?¶
In [41]:
df.loc[df["Ratio"] == 22.5]
Out[41]:
Char Count | Value | Ratio | |
---|---|---|---|
Word | |||
xu | 2 | 45 | 22.5 |
How many words have a Ratio
of 10
?¶
In [44]:
len(df.loc[df["Ratio"] == 10])
Out[44]:
2604
What is the maximum Value
of all the words with a Ratio
of 10
?¶
In [48]:
df.loc[df["Ratio"] == 10].sort_values(by=["Value"], ascending = False)
Out[48]:
Char Count | Value | Ratio | |
---|---|---|---|
Word | |||
electrocardiographically | 24 | 240 | 10.0 |
electroencephalographies | 24 | 240 | 10.0 |
electroencephalographer | 23 | 230 | 10.0 |
electrodesiccation | 18 | 180 | 10.0 |
phonocardiographic | 18 | 180 | 10.0 |
... | ... | ... | ... |
col | 3 | 30 | 10.0 |
bis | 3 | 30 | 10.0 |
sib | 3 | 30 | 10.0 |
as | 2 | 20 | 10.0 |
oe | 2 | 20 | 10.0 |
2604 rows × 3 columns
Of those words with a Value
of 260
, what is the lowest Char Count
found?¶
In [49]:
df.loc[df["Value"] == 260, "Char Count"].min()
Out[49]:
17
Based on the previous task, what word is it?¶
In [55]:
df.loc[(df["Char Count"] == 17) & (df["Value"] == 260)]
Out[55]:
Char Count | Value | Ratio | |
---|---|---|---|
Word | |||
hydroxytryptamine | 17 | 260 | 15.294118 |
In [ ]: