Jun-08-2024, 11:25 AM
In context of Pandas and NumPy, NaN(Not a Number) is considered a float as they follow IEEE 754 floating-point standard.
Pandas has of course different ways to deal with NaN values.
Pandas has of course different ways to deal with NaN values.
import pandas as pd import json pd.set_option('display.expand_frame_repr', False) data = { '客户编码': [11820122.0, 11820123.0, 11820124.0], '客户名称': ['WILLOWOOD FZE (Free Zone Establishment)', 'AgroChem Inc', 'Green Fields'], '国家': ['美国', '巴西', '南非'], '网址': [pd.NA, 'www.agrochem.com', 'www.greenfields.co.za'], '联系人': [pd.NA, 'John Doe', 'Jane Smith'], '邮箱': [pd.NA, '[email protected]', '[email protected]'], '电话': [pd.NA, '555-1234', '555-5678'], '公司地址': [pd.NA, '123 AgroChem Road, Brazil', '789 Green Fields Ave, South Africa'], '主要产品': [pd.NA, 'Fertilizers', 'Pesticides'] } df = pd.DataFrame(data) # Filter out rows where the '邮箱' (email) column is NaN df_filtered = df.dropna(subset=['邮箱']) # Save the customer list to a JSON file customer_list = df_filtered[['客户编码', '客户名称', '联系人', '邮箱']].to_dict(orient='records') with open('customers.json', 'w', encoding='utf-8') as fp: json.dump(customer_list, fp, ensure_ascii=False, indent=4) print(f'Customers with email addresses have been saved to {fp.name}')
Output:[
{
"客户编码": 11820123.0,
"客户名称": "AgroChem Inc",
"联系人": "John Doe",
"邮箱": "[email protected]"
},
{
"客户编码": 11820124.0,
"客户名称": "Green Fields",
"联系人": "Jane Smith",
"邮箱": "[email protected]"
}
]