[Python] Visualize the heat of Tokyo and XX prefectures (DataFrame usage memo)

0. Problems hidden in everyday life

Summer greets in the state of emergency. In order to avoid the Three Cs thoroughly, let alone play, I usually go through ideas I couldn't go to the cafe I was given, so I kept it at home. The only topics are the number of people infected with the coronavirus and the heat.

As a person living in Tokyo, even if I talk about how hot it is and I can't move, my cousins and father told me that the local area is just as hot and that I am doing this kind of exercise (cycling) in the hot weather. It seemed that he was competing for the heat, and he was tired.

In the news that I see from time to time, it seems that the locals in the snowy country are hitting the highest temperature as in Tokyo.

It was so hot that I thought it was steaming from the ground. Just walking for 10 minutes makes me flutter. And even if you go into a cafe and have a cold drink, you can't easily get rid of the heat that fills your body. While having a drink, I thought about how to compare the heat of Niigata and Tokyo at the highest temperature.

1. 1. Acquisition of temperature data

From Japan Meteorological Agency, we were able to obtain weather information for each region by following the procedure below. This time we need data from Niigata and Tokyo, so even if the following is for Tokyo Execute and press the "Download csv file" button.

qiita_weather_niigata1.jpg

qiita_weather_niigata2.jpg

I edited only the item part of the above csv file to make it easier to use.

2. Various issues

(1) Read the CSV file

weather_parse2.jpynb



import pandas as pd
import datetime as dt
#Library to display graphs
import matplotlib.pyplot as plt
#Japanese localization library
import japanize_matplotlib
#Specify the character code and read
df = pd.read_csv("data_weather2.csv",encoding="sjis")
print(df)

__ Output result __


Date Average temperature(℃)_Tokyo Quality Information Homogeneous Number Maximum Temperature(℃)_Tokyo quality information.1 Homogeneous number.1  \
0     2020/1/1         5.5     8     1        10.2       8       1   
1     2020/1/2         6.2     8     1        11.3       8       1   
2     2020/1/3         6.1     8     1        12.0       8       1   
3     2020/1/4         7.2     8     1        12.2       8       1   
4     2020/1/5         5.4     8     1        10.2       8       1   
..         ...         ...   ...   ...         ...     ...     ...   
229  2020/8/17        30.8     8     1        36.5       8       1   
230  2020/8/18        30.3     8     1        34.3       8       1   
231  2020/8/19        29.0     8     1        34.2       8       1   
232  2020/8/20        29.7     8     1        34.8       8       1   
233  2020/8/21        30.1     8     1        36.0       8       1   

Lowest Temperature(℃)_Tokyo quality information.2 Homogeneous number.2 Average temperature(℃)_Niigata Quality Information.3 homogeneous number.3 Maximum temperature(℃)_Niigata\
0           3.2       8       1         3.6       8       1         5.6   
1           1.9       8       1         3.0       8       1         5.7   
2           1.4       8       1         4.2       8       1         6.9   
3           3.6       8       1         5.5       8       1         8.3   
4           0.6       8       1         4.2       8       1         7.2   
..          ...     ...     ...         ...     ...     ...         ...   
229        27.2       8       1        27.8       8       1        30.4   
230        27.8       8       1        26.3       8       1        29.0   
231        25.6       8       1        26.8       8       1        30.9   
232        25.8       8       1        28.5       8       1        33.7   
233        26.0       8       1        27.5       8       1        30.8   

quality information.4 Homogeneous number.4 Minimum temperature(℃)_Niigata Quality Information.5 homogeneous number.5  
0         8       1         1.0       8       1  
1         8       1         2.0       8       1  
2         8       1         1.7       8       1  
3         8       1         2.4       8       1  
4         8       1         1.7       8       1  
..      ...     ...         ...     ...     ...  
229       8       1        26.3       8       1  
230       8       1        23.3       8       1  
231       8       1        22.0       8       1  
232       8       1        22.8       8       1  
233       8       1        23.8       8       1  

[234 rows x 19 columns]

(2) Column selection and range selection to be graphed

__ (Memo 1) To select and display column name 1 and column name 2 from the above __

Specify DataFrame [['column name 1','column name 2']].

__ (Memo 2) Creating a DataFrame __

DataFrame name = pd.DataFrame (data, columns = list of column names you want to specify)

__ (Note 3) To convert column name 3 to the default date type (yyyy-mm-dd) __

DataFrame name ['column name 3'] = pd.to_datetime (DataFrame name ['column name 3'], format ='format before change of column name 3')

weather_parse2.jpynb


new_columns = ['date', 'Highest temperature(℃)_Tokyo', 'Highest temperature(℃)_Niigata']  #Column name
new_data=df[['date','Highest temperature(℃)_Tokyo','Highest temperature(℃)_Niigata']]

#recreate dataframe
new_df=pd.DataFrame(new_data,columns=new_columns)
#If you do nothing, it will not be recognized as a date type
print(new_df.dtypes)
#Convert date column to date type
new_df['date'] = pd.to_datetime(new_df['date'], format='%Y/%m/%d')
print(new_df.dtypes)
print(new_df)

__ Output result __

Date object
Highest temperature(℃)_Tokyo float64
Highest temperature(℃)_Niigata float64
dtype: object
Datetime64[ns]
Highest temperature(℃)_Tokyo float64
Highest temperature(℃)_Niigata float64
dtype: object
Date Maximum temperature(℃)_Tokyo highest temperature(℃)_Niigata
0   2020-01-01        10.2         5.6
1   2020-01-02        11.3         5.7
2   2020-01-03        12.0         6.9
3   2020-01-04        12.2         8.3
4   2020-01-05        10.2         7.2
..         ...         ...         ...
229 2020-08-17        36.5        30.4
230 2020-08-18        34.3        29.0
231 2020-08-19        34.2        30.9
232 2020-08-20        34.8        33.7
233 2020-08-21        36.0        30.8

[234 rows x 3 columns]

weather_parse2.jpynb



new_df2=new_df[(new_df['date'] >= dt.datetime(2020,8,1)) & (new_df['date'] < dt.datetime(2020,8,21))]
print(new_df2)

__ Output result __

Date Maximum temperature(℃)_Tokyo highest temperature(℃)_Niigata
213 2020-08-01        31.8        28.8
214 2020-08-02        31.5        30.3
215 2020-08-03        32.3        32.2
216 2020-08-04        33.1        32.7
217 2020-08-05        34.2        33.4
218 2020-08-06        33.1        33.6
219 2020-08-07        35.4        28.3
220 2020-08-08        32.3        24.8
221 2020-08-09        34.7        28.4
222 2020-08-10        35.2        35.6
223 2020-08-11        37.3        35.7
224 2020-08-12        35.8        31.4
225 2020-08-13        36.1        31.4
226 2020-08-14        34.2        32.0
227 2020-08-15        36.1        30.1
228 2020-08-16        35.4        31.4
229 2020-08-17        36.5        30.4
230 2020-08-18        34.3        29.0
231 2020-08-19        34.2        30.9
232 2020-08-20        34.8        33.7

(3) Graphing

weather_parse2.jpynb



#Specify the data you want to graph
new_columns3 = ['Highest temperature(℃)_Tokyo', 'Highest temperature(℃)_Niigata']  #Column name
new_data3=new_df2[['Highest temperature(℃)_Tokyo','Highest temperature(℃)_Niigata']]

new_df3=pd.DataFrame(new_data3,columns=new_columns3)
#graph display
new_df3.plot()

matplot_weather_before.jpeg

3. 3. Completed form (specify the index as a date and output the maximum temperature for the specified period)

weather_parse3.jpynb



import pandas as pd
import datetime as dt
#Read by specifying the character code, indexing the date
df = pd.read_csv("data_weather2.csv",encoding="sjis")
#Index by date
df.index=pd.to_datetime(df['date'], format='%Y/%m/%d').values
#Delete the original column
df = df.drop(columns='date')
print(df)

Output result

Average temperature(℃)_Tokyo Quality Information Homogeneous Number Maximum Temperature(℃)_Tokyo quality information.1 Homogeneous number.1 Minimum temperature(℃)_Tokyo\
2020-01-01         5.5     8     1        10.2       8       1         3.2   
2020-01-02         6.2     8     1        11.3       8       1         1.9   
2020-01-03         6.1     8     1        12.0       8       1         1.4   
2020-01-04         7.2     8     1        12.2       8       1         3.6   
2020-01-05         5.4     8     1        10.2       8       1         0.6   
...                ...   ...   ...         ...     ...     ...         ...   
2020-08-17        30.8     8     1        36.5       8       1        27.2   
2020-08-18        30.3     8     1        34.3       8       1        27.8   
2020-08-19        29.0     8     1        34.2       8       1        25.6   
2020-08-20        29.7     8     1        34.8       8       1        25.8   
2020-08-21        30.1     8     1        36.0       8       1        26.0   

quality information.2 Homogeneous number.2 Average temperature(℃)_Niigata Quality Information.3 homogeneous number.3 Maximum temperature(℃)_Niigata Quality Information.4  \
2020-01-01       8       1         3.6       8       1         5.6       8   
2020-01-02       8       1         3.0       8       1         5.7       8   
2020-01-03       8       1         4.2       8       1         6.9       8   
2020-01-04       8       1         5.5       8       1         8.3       8   
2020-01-05       8       1         4.2       8       1         7.2       8   
...            ...     ...         ...     ...     ...         ...     ...   
2020-08-17       8       1        27.8       8       1        30.4       8   
2020-08-18       8       1        26.3       8       1        29.0       8   
2020-08-19       8       1        26.8       8       1        30.9       8   
2020-08-20       8       1        28.5       8       1        33.7       8   
2020-08-21       8       1        27.5       8       1        30.8       8   

Homogeneous number.4 Minimum temperature(℃)_Niigata Quality Information.5 homogeneous number.5  
2020-01-01       1         1.0       8       1  
2020-01-02       1         2.0       8       1  
2020-01-03       1         1.7       8       1  
2020-01-04       1         2.4       8       1  
2020-01-05       1         1.7       8       1  
...            ...         ...     ...     ...  
2020-08-17       1        26.3       8       1  
2020-08-18       1        23.3       8       1  
2020-08-19       1        22.0       8       1  
2020-08-20       1        22.8       8       1  
2020-08-21       1        23.8       8       1  

[234 rows x 18 columns]

weather_parse3.jpynb


#Cut out columns
new_columns = ['Highest temperature(℃)_Tokyo', 'Highest temperature(℃)_Niigata']  #Column name
new_data=df[['Highest temperature(℃)_Tokyo','Highest temperature(℃)_Niigata']]

#recreate dataframe
new_df=pd.DataFrame(new_data,columns=new_columns)

print(new_df)

Output result

Highest temperature(℃)_東京 Highest temperature(℃)_Niigata
2020-01-01        10.2         5.6
2020-01-02        11.3         5.7
2020-01-03        12.0         6.9
2020-01-04        12.2         8.3
2020-01-05        10.2         7.2
...                ...         ...
2020-08-17        36.5        30.4
2020-08-18        34.3        29.0
2020-08-19        34.2        30.9
2020-08-20        34.8        33.7
2020-08-21        36.0        30.8

[234 rows x 2 columns]

weather_parse3.jpynb



new_df2=new_df['2020-08-01':'2020-08-21']
print(new_df2)

Output result

Highest temperature(℃)_Tokyo highest temperature(℃)_Niigata
2020-08-01        31.8        28.8
2020-08-02        31.5        30.3
2020-08-03        32.3        32.2
2020-08-04        33.1        32.7
2020-08-05        34.2        33.4
2020-08-06        33.1        33.6
2020-08-07        35.4        28.3
2020-08-08        32.3        24.8
2020-08-09        34.7        28.4
2020-08-10        35.2        35.6
2020-08-11        37.3        35.7
2020-08-12        35.8        31.4
2020-08-13        36.1        31.4
2020-08-14        34.2        32.0
2020-08-15        36.1        30.1
2020-08-16        35.4        31.4
2020-08-17        36.5        30.4
2020-08-18        34.3        29.0
2020-08-19        34.2        30.9
2020-08-20        34.8        33.7
2020-08-21        36.0        30.8

weather_parse3.jpynb



colorlist=["red","blue"]
#Bar graph display in specified color
new_df2.plot.bar(color=colorlist)
#Specify the display position of the legend at the bottom right
plt.legend(loc="lower right")
plt.show()

Output result

matplot_weather.jpeg

Conclusion

The maximum temperature is often higher in Tokyo.

Recommended Posts

[Python] Visualize the heat of Tokyo and XX prefectures (DataFrame usage memo)
Python --Explanation and usage summary of the top 24 packages
Visualize the range of interpolation and extrapolation with python
The story of Python and the story of NaN
[Python] Operation memo of pandas DataFrame
Summary of the differences between PHP and Python
2016 The University of Tokyo Mathematics Solved with Python
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
Compare the speed of Python append and map
Organize the super-basic usage of Autotools and pkg-config
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
[Python] Class type and usage of datetime module
Basic operation of Python Pandas Series and Dataframe (1)
Get the update date of the Python memo file.
The story of Python without increment and decrement operators.
The process of installing Atom and getting Python running
Referencing and changing the upper bound of Python recursion
The pain of gRPC using Python. November 2019. (Personal memo)
I checked out the versions of Blender and Python
[Introduction to Python] Basic usage of the library matplotlib
Usage of Python locals ()
the zen of Python
Try scraping the data of COVID-19 in Tokyo with Python
[Python] Heron's formula functionalization and calculation of the maximum area
[Memo] The mystery of cumulative assignment statements in Python functions
[python] plot the values ​​before and after the conversion of yeojohnson conversion
[Python] I tried to visualize the follow relationship of Twitter
The process of making Python code object-oriented and improving it
[Python] Strengths and weaknesses of DataFrame in terms of time required
The websocket of toio (nodejs) and python / websocket do not connect.
I want to know the features of Python and pip
[Tips] Problems and solutions in the development of python + kivy
Play with the password mechanism of GitHub Webhook and Python
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
python memo: enumerate () -get index and element of list at the same time and turn for statement
[Python] Correct usage of map
Towards the retirement of Python2
About the ease of Python
Python and ruby slice memo
Sample usage of Python pickle
Visualize the orbit of Hayabusa2
Basic usage of Python f-string
[Python] Correct usage of join
About the features of Python
Source installation and installation of Python
The Power of Pandas: Python
I compared the speed of Hash with Topaz, Ruby and Python
Count the number of Thai and Arabic characters well in Python
Note: Get the first and last items of Python OrderedDict non-destructively
[Python] df Read and do the first memo (NaN confirmation etc.)
[Introduction to Python] I compared the naming conventions of C # and Python.
About Boxplot and Violinplot that visualize the variability of independent data
[Python] How to get the first and last days of the month
[Python] I thoroughly explained the theory and implementation of logistic regression
The story of running python and displaying the results without closing vim
[Python] I thoroughly explained the theory and implementation of decision trees
Practice of data analysis by Python and pandas (Tokyo COVID-19 data edition)
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
Get and set the value of the dropdown menu using Python and Selenium