Skip to content

Instantly share code, notes, and snippets.

@navinthenapster
Last active August 23, 2021 16:13
Show Gist options
  • Save navinthenapster/ab1274e8a8fe31fc6734fbb646cd3e02 to your computer and use it in GitHub Desktop.
Save navinthenapster/ab1274e8a8fe31fc6734fbb646cd3e02 to your computer and use it in GitHub Desktop.
Tips and Tricks for pandas operation
# to read data
df_data=pd.read_csv("csvfile.csv")
#replace value in columns
df_data = df_data.replace({
'colname': {
'value': 'new_value'
}
})
# drop columns
df_data.drop(['colname'], inplace=True, axis = 1)
# remove list of rows of certain values
df_data = df_data[~df_data['column'].isin( ['values'] )]
# to see count of each values in columns
df_data.colname.value_counts()
# to see summary
df_data.head(5)
#to see shape
df_data.shape
#to see values of columns
list(df_data['colname'].values)
# table transformation to (row_id A 1.0 )
df_data = pd.melt(df_data, id_vars='key_id', var_name='colname', value_name='value').sort_values('key_id').reset_index(drop=True)
#filling na
df_data['colname'] = df_data['colname'].fillna(value = 'value')
#-------------------------------------- GRAPH ----------------------------------------------------------------#
# https://www.shanelynn.ie/bar-plots-in-python-using-pandas-dataframes/
# draw bar and write value
ax=df_data_train.diagnosis.value_counts().plot.bar(align='edge')
for rect in ax.patches:
y_value=rect.get_height()
x_value = rect.get_x() + rect.get_width() / 2
# Number of points between bar and label. Change to your liking.
space = 10
# Vertical alignment for positive values
va = 'bottom'
#ha='left'
# If value of bar is negative: Place label below bar
if x_value < 0:
# Invert space to place label below
space *= -1
# Vertically align label at top
va = 'top'
#ha='right'
# Use Y value as label and format number with one decimal place
label = "{}".format(int(y_value))
ax.annotate(
label, # Use `label` as label
(x_value, y_value), # Place label at end of the bar
xytext=(0, space), # Vertically shift label by `space`
textcoords="offset points", # Interpret `xytext` as offset in points
ha='center', # Horizontally center label
va=va) # Vertically align label differently for
# positive and negative values.
# plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment