df = pd.read_csv(DATASET_FILENAME, sep=";")
把Pandas讀檔那行改成這樣看看,我看起來是因為你的csv是用分號分開,不是用預設的逗號。所以你讀進來的DataFrame實際上對電腦來說只有一個column。
Mathematics & Statistics
大学生・専門学校生・社会人
求助幫忙
目的是讀入資料,依性別計算身高、體重、年齡的平均,將結果寫入csv 檔案
現在目前我已經把資料成功讀入了,但到計算平均就卡住了 請各位大神幫我看看是那邊出錯了 感謝🙏
}
]
v 20 秒
0
秒
from google. colab import drive
drive. mount('/content/drive')
Mounted at /content/drive
[6] import pandas as pd
data = pd. read_csv('/dataset_1 (3).csv')
print(data)
0
1
2
3
4
539
540
541
542
543
height; "weight"; "age"; "male"
151.765;47.8256065;63;1
139.7;36.4858065:63:0
136.525;31.864838;65;0
156.845;53. 0419145;41;1
145. 415;41. 276872;51;0
145. 415;31. 127751;17;1
162.56;52. 16308;31;1
156. 21;54. 0624965;21;0
71. 12;8. 051258;0;1
158.75;52. 5316235;68;1
[544 rows x 1 columns]
✓ [7] import numpy as np
0
DATASET_FILENAME =
秒
✓ [8]
0
ply
0
/dataset_1 (3).csv'
data = np. loadtxt (DATASET_FILENAME, delimiter="; ", skiprows=1)
data. shape
type (data)
numpy. ndarray
import pandas as pd
df = pd. read_csv (DATASET_FILENAME)
"
[9] df f = df[df["male"] == 0][["height", "weight", "age"]].mean()
df_m= df [df ["male"]
1] [["height", "weight", "age"]].mean()
KeyError
Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3360
-> 3361
3362
==
KeyError: 'male'
try:
return self._engine.get_loc (casted_key)
except KeyError as err:
◆ 4 frames
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable. PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi
in pandas._libs.hashtable. PyObjectHashTable.get_item()
[9]
Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
KeyError
3360
-> 3361
3362
KeyError: 'male'
4 frames
pandas/_libs/hashtable_class_helper.pxi
in pandas._libs.hashtable. PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas. _libs.hashtable. PyObjectHashTable.get_item()
try:
The above exception was the direct cause of the following exception:
KeyError
return self._engine.get_loc (casted_key)
except KeyError as err:
Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361
3362
-> 3363
3364
3365
KeyError: 'male'
return self._engine.get_loc(casted_key)
raise KeyError(key) from err
if is_scalar (key) and isna (key) and not self.hasnans:
except KeyError as err:
SEARCH STACK OVERFLOW
回答
疑問は解決しましたか?
この質問を見ている人は
こちらの質問も見ています😉
おすすめノート
Python 03 - 選擇敘述
25
0
Python
4
0