Below are some tips for pandas developers
Dropping a column
df.drop(df.columns[idx], axis=1, inplace=True)
Setting the index column to the dataframe
df = pd.DataFrame.from_dict({
'Name': ['Jane', 'Nik', 'Kate', 'Melissa', 'Evan', 'Doug', 'Joe'],
'Age': [10, 35, 34, 23, 70, 55, 89],
'Height': [130, 178, 155, 133, 195, 150, 205],
'Weight': [80, 200, 220, 150, 140, 95, 180]
}).set_index('Name')
Group By in data frame
grouped = df.groupby("Age", axis="columns")
Selecting multiple columns
df2 = df[["Courses","Fee","Duration"]]
Using loc[] to take column slices
df2 = df.loc[:, ["Courses","Fee","Duration"]]
df2 = df.loc[:, ["Courses","Fee","Discount"]]
df2 = df.loc[:,'Fee':'Discount']
df2 = df.loc[:,'Duration':]
df2 = df.loc[:,:'Duration']
df2 = df.loc[:,::2]
Using iloc[] to select column by Index
df2 = df.iloc[:,[1,3,4]] # Select columns by Index
df2 = df.iloc[:,1:4] # Select between indexes 1 and 4 (2,3,4)
df2 = df.iloc[:,2:] # Select From 3rd to end
df2 = df.iloc[:,:2] # Select First Two Colum
DataFrame to a list
df = pd.DataFrame(data_dict)
print(f"DataFrame:\n{df}\n")
print(f"column types:\n{df.dtypes}")
col_one_list = df['one'].tolist()
DataFrame to an array
col_one_arr = df['one'].to_numpy()
Replacing all values in a dataframe based on condition
df.loc[df['First Season'] > 1990, 'First Season'] = 1
Changing the series data type to numeric
s = pd.Series(['1.0', '2', -3])
pd.to_numeric(s)
Top comments (1)
OSM