[英]How to check and add missing headers to a csv file
import pandas as pd
file_path='Yourfile.csv' #file name
df = pd.read_csv(file_path) #reading csv file
df = df.fillna(0) #replace NaN to 0
不幸的是,我知道沒有比 csv 文件中存在的列更多的干凈直接的方法來聲明更多的列。
但是一旦你得到了 Pandas DataFrame,很容易添加缺失的列:
# read a CSV file having missing columns:
tmp = pd.read_csv('file.csv')
#create an empty dataframe with all the expected columns
df = pd.DataFrame(columns=['Location', 'Total', 'Open', 'Checkin', 'Closed', 'Cancelled'])
# just copy the data:
df[tmp.columns] = tmp
就這樣。 缺失的列將用 NaN 值填充。
import pandas as pd
#Read the first excel file content
df1 = pd.read_excel(r'/content/MainFile.xlsx')
#Read the second excel file content where headers are missing
df2 = pd.read_excel(r'/content/ClientFile.xlsx')
現在查找兩個文件之間的不同列並將它們提取到一個新的數據框中:
extracted_col= df1[df1.columns.difference(df2.columns)]
然后將提取的列加入到您丟失的頭文件中。
df2=df2.join(extracted_col)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.