Pythonでパンダを使用したTRAIからのモバイルデータ速度の分析
このチュートリアルでは、pandasパッケージを使用してモバイルデータの速度を分析します。 TRAIからモバイル速度をダウンロードします 公式ウェブサイト。ファイルをダウンロードする手順。
アルゴリズム
1. Go to the [TRAI](https://myspeed.trai.gov.in/ ) website. 2. Scroll down to the end of the page. 3. You will find mobile speed data for different months. 4. Download the September mobile data speeds.
CSVの列を見てみましょう ファイル。
-
ネットワーク名
-
ネットワークテクノロジー
-
テストの種類
-
スピード
-
信号強度
-
状態
例
# importing requires libraries import pandas as pd import numpy as np import matplotlib.pyplot as plot # constants DATASET = 'sept19_publish.csv' NETWORK_NAME = 'JIO' STATE = 'Andhra Pradesh' # lists to store the values download_speeds = [] upload_speeds = [] states = [] operators = [] # importing the dataset using pandas data_frame = pd.read_csv(DATASET) # assigning column names for easy access data_frame.columns = ['Network', 'Technology', 'Type Of Test', 'Speed', 'Signal Str ength', 'State'] # getting unique states and operators from the dataset unique_states = data_frame['State'].unique() unique_operators = data_frame['Network'].unique() print(unique_states) print() print(unique_operators)
出力
上記のプログラムを実行すると、次の結果が得られます。
['Kolkata' 'Punjab' 'Delhi' 'UP West' 'Haryana' nan 'West Bengal' 'Tamil Nadu' 'Kerala' 'Rajasthan' 'Gujarat' 'Maharashtra' 'Chennai' 'Madhya Pradesh' 'UP East' 'Karnataka' 'Orissa' 'Andhra Pradesh' 'Bihar' 'Mumbai' 'North East' 'Himachal Pradesh' 'Assam' 'Jammu & Kashmir'] ['JIO' 'AIRTEL' 'VODAFONE' 'IDEA' 'CELLONE' 'DOLPHIN']
続行...
# getting the data related to one network that we want
# we already declared the network previously
# this filtering the data
JIO = data_frame[data_frame['Network'] == NETWORK_NAME]
# iterating through the all states
for state in unique_states:
# getting all the data of current state
current_state = JIO[JIO['State'] == state]
# getting download speed from the current_state
download_speed = current_state[current_state['Type Of Test'] == 'download']
# calculating download_speed average
download_speed_avg = download_speed['Speed'].mean()
# getting upload speed from the current_state
upload_speed = current_state[current_state['Type Of Test'] == 'upload']
# calculating upload_speed average
upload_speed_avg = upload_speed['Speed'].mean()
# checking if the averages or nan or not
if pd.isnull(download_speed_avg) or pd.isnull(upload_speed_avg):
# assigning zeroes to the both speeds
download_speed, upload_speed = 0, 0
else:
# appending state if the values are not nan to plot
states.append(state)
download_speeds.append(download_speed_avg)
upload_speeds.append(upload_speed_avg)
# printing the download ans upload averages
print(f'{state}: Download Avg. {download_speed_avg:.3f} Upload Avg. {upload _speed_avg:.3f}') 上記のコードを実行すると、次の結果が得られます。
Kolkata: Download Avg. 31179.157 Upload Avg. 5597.086 Punjab: Download Avg. 29289.594 Upload Avg. 5848.015 Delhi: Download Avg. 28956.174 Upload Avg. 5340.927 UP West: Download Avg. 21666.673 Upload Avg. 4118.200 Haryana: Download Avg. 6226.855 Upload Avg. 2372.987 West Bengal: Download Avg. 20457.976 Upload Avg. 4219.467 Tamil Nadu: Download Avg. 24029.364 Upload Avg. 4269.765 Kerala: Download Avg. 10735.611 Upload Avg. 2088.881 Rajasthan: Download Avg. 26718.066 Upload Avg. 5800.989 Gujarat: Download Avg. 16483.987 Upload Avg. 3414.485 Maharashtra: Download Avg. 20615.311 Upload Avg. 4033.843 Chennai: Download Avg. 6244.756 Upload Avg. 2271.318 Madhya Pradesh: Download Avg. 15757.381 Upload Avg. 3859.596 UP East: Download Avg. 28827.914 Upload Avg. 5363.082 Karnataka: Download Avg. 10257.426 Upload Avg. 2584.806 Orissa: Download Avg. 32820.872 Upload Avg. 5258.215 Andhra Pradesh: Download Avg. 8260.547 Upload Avg. 2390.845 Bihar: Download Avg. 9657.874 Upload Avg. 3197.166 Mumbai: Download Avg. 9984.954 Upload Avg. 3484.052 North East: Download Avg. 4472.731 Upload Avg. 2356.284 Himachal Pradesh: Download Avg. 6985.774 Upload Avg. 3970.431 Assam: Download Avg. 4343.987 Upload Avg. 2237.143 Jammu & Kashmir: Download Avg. 1665.425 Upload Avg. 802.925
続行...
# plotting the graph'
fix, axes = plot.subplots()
# setting bar width
bar_width = 0.25
# rearranging the positions of states
re_states = np.arange(len(states))
# setting the width and height
plot.figure(num = None, figsize = (12, 5))
# plotting the download spped
plot.bar(re_states, download_speeds, bar_width, color = 'g', label = 'Avg. Download
Speed')
# plotting the upload speed
plot.bar(re_states + bar_width, upload_speeds, bar_width, color='b', label='Avg. Up
load Speed')
# title of the graph
plot.title('Avg. Download|Upload Speed for ' + NETWORK_NAME)
# x-axis label
plot.xlabel('States')
# y-axis label
plot.ylabel('Average Speeds in Kbps')
# the label below each of the bars,
# corresponding to the states
plot.xticks(re_states + bar_width, states, rotation = 90)
# draw the legend
plot.legend()
# make the graph layout tight
plot.tight_layout()
# show the graph
plot.show() 上記のグラフを実行すると、次のグラフが表示されます。
結論
ニーズに基づいてさまざまなグラフをプロットできます。さまざまなグラフをプロットして、データセットを試してみてください。チュートリアルについて疑問がある場合は、コメントセクションにその旨を記載してください。
-
Python-Pandas .query()メソッドを使用したデータのフィルタリング
Pandasは、データクレンジング、データ分析などに非常に広く使用されているPythonライブラリです。この記事では、クエリメソッドを使用して特定のデータセットから特定のデータをフェッチする方法を説明します。クエリ内に単一の条件と複数の条件の両方を含めることができます。 データの読み取り まず、pandasライブラリを使用してデータをpandasデータフレームに読み込みます。以下のプログラムはそれを実行します。 例 import pandas as pd # Reading data frame from csv file data = pd.read_csv("D:\\hear
-
Pythonでのデータ分析と視覚化?
Pythonは、主にnumpy、pandas、matplotlib、seabornなどのデータ分析と視覚化のための多数のライブラリを提供します。このセクションでは、numpyの上に構築されたオープンソースライブラリであるデータ分析と視覚化のためのpandasライブラリについて説明します。 これにより、迅速な分析とデータのクリーニングと準備を行うことができます。Pandasには、以下で説明する多数の組み込みの視覚化機能も用意されています。 インストール パンダをインストールするには、ターミナルで以下のコマンドを実行します- pipinstall pandas Orweにはanacondaが