Skip to content

Commit 603846b

Browse files
committed
Update chapter7.md
- Change pd.set_option('display.width', 5000) to pd.set_option('display.width', 5000) for deprecation fix - Use requests.loc to replace requests['Incident Zip'] with value '00000' with np.nan for addressing pandas copy warnings - Delete unique_zips.sort() because of error sorting array of strings and nan - Add .dropna() - Change .sort() to sort_values()
1 parent 193282e commit 603846b

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

content/pandas cookbook/chapter7.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ plt.style.use('default')
2525
figsize(15, 5)
2626

2727
# Always display all the columns
28-
pd.set_option('display.line_width', 5000)
28+
pd.set_option('display.width', 5000)
2929
pd.set_option('display.max_columns', 60)
3030
```
3131

@@ -767,15 +767,13 @@ Output:
767767
This looks bad to me. Let's set these to nan.
768768

769769
```python
770-
zero_zips = requests['Incident Zip'] == '00000'
771-
requests['Incident Zip'][zero_zips] = np.nan
770+
requests.loc[requests['Incident Zip'] == '00000', 'Incident Zip'] = np.nan
772771
```
773772

774773
Great. Let's see where we are now:
775774

776775
```python
777776
unique_zips = requests['Incident Zip'].unique()
778-
unique_zips.sort()
779777
unique_zips
780778
```
781779

@@ -829,7 +827,7 @@ zips = requests['Incident Zip']
829827
is_close = zips.str.startswith('0') | zips.str.startswith('1')
830828
# There are a bunch of NaNs, but we're not interested in them right now, so we'll say they're True
831829
is_far = ~(is_close.fillna(True).astype(bool))
832-
zips[is_far]
830+
zips.loc[is_far].dropna()
833831
```
834832

835833
Output:
@@ -955,7 +953,7 @@ Output:
955953
Okay, there really are requests coming from LA and Houston! Good to know. Filtering by zip code is probably a bad way to handle this -- we should really be looking at the city instead.
956954

957955
```python
958-
requests['City'].str.upper().value_counts()
956+
requests[is_far][['Incident Zip', 'Descriptor', 'City']].dropna().sort_values('Incident Zip')
959957
```
960958

961959
Output:

0 commit comments

Comments
 (0)