JSON [Ký hiệu đối tượng JavaScript] là cách nhiều thông tin được truyền qua internet. May mắn là có một thư viện tên là
# create a new column or change 0 to 1 if keyword exists9 đi kèm với thư viện chuẩn của Python. Điều này có nghĩa là nếu bạn đã cài đặt Python thì bạn đã có mô-đun này. Nếu bạn muốn tìm hiểu thêm, hãy xem tài liệu
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Bằng cách nhập gói
# create a new column or change 0 to 1 if keyword exists9, chúng ta có thể biến tất cả các đối tượng JSON thành kiểu dữ liệu Python tương ứng của chúng. Python và Pandas sẽ không cho bạn biết thứ gì đó là JSON một cách rõ ràng, nhưng điều này thường rất dễ xác định xem bạn có dữ liệu lồng nhau trong dấu ngoặc nhọn [
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists1] được chuyển thành loại
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists2 hay không
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Đây là mã tôi đã viết để giải mã JSON của mình thành Python
Bước 2. Thể hiện dữ liệu JSON trên nhiều cột
Không có gì chúng tôi đã làm là hữu ích trừ khi chúng tôi có thể trích xuất dữ liệu từ JSON. Để làm điều này, tôi đã tạo một hàm có thể được sử dụng với phương pháp Pandas
# create a new column or change 0 to 1 if keyword exists3 và được áp dụng theo hàng chứ không phải theo cột [
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists4]
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Ý tưởng của tôi là mã hóa dữ liệu một lần để duy trì định dạng gọn gàng. Điều này có nghĩa là mỗi hàng đại diện cho một quan sát và mỗi cột đại diện cho một thuộc tính của quan sát
Tôi đã tạo một cột mới cho mỗi khóa và giá trị được nhìn thấy trong dữ liệu và nếu khóa hoặc giá trị được nhìn thấy trong quan sát quan tâm thì điều này được biểu thị bằng
# create a new column or change 0 to 1 if keyword exists5 nếu nó không được nhìn thấy thì thay vào đó, một
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists0 được sử dụng
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Đây là mã
Hãy giải thích mã đang làm gì, đầu tiên chúng ta lặp qua độ dài của danh sách từ điển và tạo một danh sách để lưu trữ các cặp
# create a new column or change 0 to 1 if keyword exists1. Sau đó, chúng tôi lặp lại từng từ điển và trích xuất
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists2 và
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists3 bằng cách sử dụng
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists4.
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists5 sẽ trả về một danh sách các bộ trong đó phần tử đầu tiên là
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists2 và phần tử thứ hai là
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists7 được liên kết. Từ đây, chúng ta sẽ nối
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists2 và
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists7 cùng với dấu gạch dưới vào đối tượng
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists00 của chúng ta
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists1
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Tiếp theo, chúng ta sẽ tạo một cột mới cho mỗi sự kết hợp của các cặp
# create a new column or change 0 to 1 if keyword exists1 được thấy trong đối tượng
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists00. Nếu cột không tồn tại thì chúng tôi sẽ tạo một cột mới với tên được liên kết và đặt tất cả các giá trị thành
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists0 trong khi thay đổi giá trị của quan sát hiện tại thành
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists5
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
# create a new column or change 0 to 1 if keyword exists05 trả về chỉ số của quan sát hiện tại. Khi
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return# create a new column or change 0 to 1 if keyword exists4, điều này sẽ trả về các giá trị chỉ mục khi
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return# create a new column or change 0 to 1 if keyword exists07, điều này sẽ trả về tên cột. Xem các tài liệu ở đây
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Bước 3. Áp dụng chức năng
# create a new column or change 0 to 1 if keyword exists0
for z in range[len[ls]]:# If column not in the df columns then make a new column and assign zero values while changing the current row to 1
if ls[z] not in df.columns:
df[ls[z]] = 0
df[ls[z]].iloc[x.name] = 1
else:
df[ls[z]].iloc[x.name] = 1
return
Những suy nghĩ cuối cùng
Mã trả về một DataFrame có hơn 29.000 cột và mất nhiều thời gian để chạy. Bạn có thể muốn thử nghiệm trên một tập hợp con dữ liệu của mình trước khi thử nghiệm trên tất cả dữ liệu của mình
Trước khi sử dụng mã này, bạn có thể tìm kiếm từng cột JSON để biết có bao nhiêu giá trị duy nhất trước khi sử dụng mã. Bạn luôn có thể truyền các giá trị ít thấy hơn là 'Khác' để giúp tăng tốc thời gian chạy
Vậy là xong, làm việc với dữ liệu JSON trong Pandas không tệ lắm và với một chút sáng tạo, bạn có thể viết mã để thực hiện bất cứ điều gì trái tim bạn mong muốn. Vui lòng điều chỉnh mã để mã không lưu các giá trị hoặc khóa nhất định. Mã được cung cấp nhằm phục vụ dưới dạng bản soạn sẵn cho bất kỳ vấn đề nào liên quan đến JSON mà bạn đang xử lý