import pandas as pd ng_list1 = [{"name": "Jake", "grade": 80}, {"name": "Annie", "grade": 90}, {"name": "Jane"}] ng_list2 = [{"name": "Sam", "grade": 74}, {"name": "Ben", "grade": 64}, {"name": "Sky", "grade": 77}, {"name": "Annie", "grade": 90}] # create data frame df1 = pd.DataFrame(ng_list1) df2 = pd.DataFrame(ng_list2) print(df1, "\n") print(df2, "\n") # concatenate df1 and df2 df = pd.concat([df1, df2], ignore_index=True) print(df, "\n") # remove duplicates df.drop_duplicates(inplace=True, ignore_index=True) # dealing with NaN # if you wanna drop any row including NaN, use df.dropna(inplace=True) # if you wanna drop a row with NaN for all columns, use df.dropna(how="all", inplace=True) # if you wanna fill all NaN with 0, use df.fillna(0, inplace=True) df.fillna(0, inplace=True) print(df, "\n")
Aug 15, 2021
[Python] concatenate two data frame, and deal with NaN
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment