Python的数据处理

  • python
  • data

posted on 22 Aug 2017 under category 人生经验

pandas

concat two data frames

df_all = pd.concat([df1, df2])

select columns

df_some_columns = df[["col1", "col2"]]

merge two data frames

df_merged = pd.merge(df1, df2, on="id", how="left")

copy a data frame

df_copy = df.copy()

unique

df["id"].unique() # get unique id.
df["id"].nunique() # get number of unique id.

NaN

df.isnull() # get a data frame which elements are true/false.
df.isnull().sum() # get a series which indexes are column names, values are number of null elements in this column.
df.isnull().sum().sum() # get a integer which is the number of null elements in data frame.