|
import pandas as pd |
|
import json |
|
from sklearn.model_selection import train_test_split |
|
|
|
|
|
with open('data.json', 'r', encoding='utf-8') as f: |
|
data = json.load(f) |
|
|
|
|
|
names = list(data.keys()) |
|
images = [item.get('images', []) for item in data.values()] |
|
|
|
|
|
details_columns = {key: [] for key in {k for v in data.values() for k in v.get('details', {})}} |
|
|
|
for value in data.values(): |
|
for detail_key in details_columns: |
|
details_columns[detail_key].append(value.get('details', {}).get(detail_key)) |
|
|
|
|
|
columns = { |
|
"Name": names, |
|
"Images": images, |
|
**details_columns |
|
} |
|
df = pd.DataFrame(columns) |
|
|
|
|
|
def generate_html(image_paths): |
|
|
|
if isinstance(image_paths, str): |
|
image_paths = eval(image_paths) |
|
elif not isinstance(image_paths, list): |
|
return None |
|
|
|
return ''.join([f'<img src="{path}" style="width:100px; margin:5px;" />' for path in image_paths]) |
|
|
|
|
|
df['Images_HTML'] = df['Images'].apply(generate_html) |
|
|
|
|
|
|
|
|
|
df_flattened = df.dropna(axis=1, how='all') |
|
|
|
|
|
df_flattened = df_flattened.drop(columns=['Charging portsLog in to see.'], errors='ignore') |
|
|
|
df_flattened = df_flattened.loc[:, df_flattened.notna().sum() >= 10] |
|
|
|
|
|
df_flattened.to_parquet('train_with_html.parquet', index=False) |
|
|
|
print("Data has been flattened, split, and saved to 'train.csv', 'test.csv', and 'val.csv'.") |
|
print("Train dataset with HTML column saved to 'train_with_html.parquet'.") |
|
|