Pandas: แปลง dataframe เป็นพจนานุกรมอย่างรวดเร็ว
คุณสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อแปลง DataFrame แพนด้าเป็นพจนานุกรม:
df. to_dict ()
โปรดทราบว่า to_dict() ยอมรับข้อโต้แย้งที่เป็นไปได้ต่อไปนี้:
- dict: (ค่าเริ่มต้น) คีย์คือชื่อคอลัมน์ ค่าต่างๆ เป็นพจนานุกรมของคู่ดัชนี:ข้อมูล
- รายการ: คีย์คือชื่อคอลัมน์ ค่าคือรายการข้อมูลคอลัมน์
- series: คีย์คือชื่อคอลัมน์ ค่าเป็นชุดข้อมูลคอลัมน์
- แยก: คีย์คือ “คอลัมน์”, “ข้อมูล” และ “ดัชนี”
- บันทึก: คีย์คือชื่อคอลัมน์ ค่าคือข้อมูลในเซลล์
- ดัชนี: คีย์คือป้ายกำกับดัชนี ค่าคือข้อมูลในเซลล์
ตัวอย่างต่อไปนี้แสดงวิธีการใช้ไวยากรณ์นี้ในทางปฏิบัติกับ Pandas DataFrame ต่อไปนี้:
import pandas as pd #createDataFrame df = pd. DataFrame ({' team ': ['A', 'A', 'B', 'B', 'C'], ' points ': [5, 7, 9, 12, 9], ' rebounds ': [11, 8, 6, 6, 5]}) #view DataFrame df team points rebounds 0 to 5 11 1 to 7 8 2 B 9 6 3 B 12 6 4 C 9 5
ตัวอย่างที่ 1: แปลง DataFrame เป็นพจนานุกรม (“dict”)
รหัสต่อไปนี้แสดงวิธีการแปลง DataFrame ของ pandas เป็นพจนานุกรมโดยใช้วิธีเริ่มต้น ‘ dict ‘:
df. to_dict ()
{'team': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'C'},
'points': {0:5, 1:7, 2:9, 3:12, 4:9},
'rebounds': {0:11, 1:8, 2:6, 3:6, 4:5}}
ตัวอย่างที่ 2: แปลง DataFrame เป็นพจนานุกรม (“รายการ”)
รหัสต่อไปนี้แสดงวิธีการแปลง DataFrame ของ pandas เป็นพจนานุกรมโดยใช้วิธี ‘ list ‘:
df. to_dict (' list ') {'team': ['A', 'A', 'B', 'B', 'C'], 'points': [5, 7, 9, 12, 9], 'rebounds': [11, 8, 6, 6, 5]}
ตัวอย่างที่ 3: แปลง DataFrame เป็นพจนานุกรม (“ซีรีส์”)
รหัสต่อไปนี้แสดงวิธีการแปลง DataFrame ของ pandas เป็นพจนานุกรมโดยใช้วิธี ‘ series ‘:
df. to_dict (' series ') {'team': 0 A 1A 2 B 3 B 4C Name: team, dtype: object, 'points': 0 5 1 7 2 9 3 12 4 9 Name: points, dtype: int64, 'rebounds': 0 11 1 8 2 6 3 6 4 5 Name: rebounds, dtype: int64}
ตัวอย่างที่ 4: แปลง DataFrame เป็นพจนานุกรม (“แยก”)
รหัสต่อไปนี้แสดงวิธีการแปลง DataFrame ของ pandas เป็นพจนานุกรมโดยใช้วิธี ‘ split ‘:
df. to_dict (' split ') {'index': [0, 1, 2, 3, 4], 'columns': ['team', 'points', 'rebounds'], 'data': [['A', 5, 11], ['A', 7, 8], ['B', 9, 6], ['B', 12, 6], ['C', 9, 5]]}
ตัวอย่างที่ 5: แปลง DataFrame เป็นพจนานุกรม (“บันทึก”)
รหัสต่อไปนี้แสดงวิธีการแปลง DataFrame ของ pandas เป็นพจนานุกรมโดยใช้วิธี ‘ records ‘:
df. to_dict (' records ') [{'team': 'A', 'points': 5, 'rebounds': 11}, {'team': 'A', 'points': 7, 'rebounds': 8}, {'team': 'B', 'points': 9, 'rebounds': 6}, {'team': 'B', 'points': 12, 'rebounds': 6}, {'team': 'C', 'points': 9, 'rebounds': 5}]
ตัวอย่างที่ 6: แปลง DataFrame เป็นพจนานุกรม (“ดัชนี”)
รหัสต่อไปนี้แสดงวิธีการแปลง DataFrame ของ pandas เป็นพจนานุกรมโดยใช้วิธี ‘ index ‘:
df. to_dict (' index ') {0: {'team': 'A', 'points': 5, 'rebounds': 11}, 1: {'team': 'A', 'points': 7, 'rebounds': 8}, 2: {'team': 'B', 'points': 9, 'rebounds': 6}, 3: {'team': 'B', 'points': 12, 'rebounds': 6}, 4: {'team': 'C', 'points': 9, 'rebounds': 5}}
แหล่งข้อมูลเพิ่มเติม
บทช่วยสอนต่อไปนี้จะอธิบายวิธีการแปลงข้อมูลทั่วไปอื่นๆ ในแพนด้า:
วิธีแปลง Pandas DataFrame เป็นอาร์เรย์ NumPy
วิธีแปลงซีรีย์ Pandas เป็นอาร์เรย์ NumPy
วิธีแปลง Pandas DataFrame เป็น List