Python Pandas -添加头与csv匹配的记录

ctzwtxfj  于 2023-07-31  发布在  Python
关注(0)|答案(1)|浏览(90)

我有一个使用Pandas将JSON数据导出到CSV的Python脚本。最近网上的数据发生了变化,我没有意识到这一点,新的记录与标题不符。
是否可以让Pandas检查头并以正确的格式将记录更新为CSV?或者你能提出一个更好的方法来做到这一点吗?
谢谢大家!

v1l68za4

v1l68za41#

我看到:

df = pd.read_json("your_json_file.json") # load your json data

required_columns = ["col1", "col2", "col3"]  # replace with your actual column headers

for col in required_columns:
    if col not in df.columns:
        df[col] = None # or replace with some default value

字符串

相关问题