Data Science (Pandas)
Removing row by using the index position [Link]([Link][50])
.dropna() Used to remove row have NaN
To access a particular column from a data frame we can use the square bracket
notation, like so:
1. clean_df['Starting Median Salary']
2. To find the highest starting salary we can simply chain the .max() method.
3. .idxmax() method will give us index for the row with the largest value.
Here we are selecting both a column ('Undergraduate Major') and a row at index
43, so we are retrieving the value of a particular cell.
1. clean_df['Undergraduate Major'][43]
2.clean_df['Undergraduate Major'].loc[43]
To retrieve an entire row at particular index: clean_df.loc[43]
Adding Columns
spread_col = clean_df['Mid-Career 90th Percentile Salary'] - clean_df['Mid-
Career 10th Percentile Salary']
clean_df.insert(1, 'Spread', spread_col)
clean_df.head()
Sorting Values
sort_values() sort in ascending or descending order
low_risk = clean_df.sort_values('Spread', ascending=False)
low_risk[['Undergraduate Major', 'Spread']].head()
Grouping and Pivoting Data with Pandas
clean_df.groupby('Group').count()
[Link].float_format = '{:,.2f}'.format
print(cleaned_data.groupby('Group').mean(numeric_only=True))
CSV has a header but you want to replace column names
df = pd.read_csv("[Link]")
[Link] = ["id", "name", "age", "marks"]
to_datetime()
[Link] = pd.to_datetime(data['DATE'])
print([Link]())
Data Manipulation: Pivoting DataFrames
Pivot methods are used to reshape and summarize data by converting rows
into columns, making data easier to analyze and compare.
pivoted_df = test_df.pivot(index='Age', columns='Actor', values='Power')
reshaped_df.fillna(0, inplace=True) -- > Used to fill NaN values
reshaped_df.isna().[Link]() check if there are any NaN values left in the
entire DataFrame.
Save DataFrame
To save dataframe in another file
reshaped_data.to_excel("reshaped_data.xlsx")
Pandas .agg() function
theme_by_year=updated_df2.groupby('year').agg({'theme_id':
[Link]})
.agg() method takes a dictionary as an argument. In this dictionary, we specify
which operation we'd like to apply to each column. In our case, we just want to
calculate the number of unique entries in the theme_id column by using our old
friend, the .nunique() method.
Superimposing Line Charts with Separate Axes
ax1 = [Link]() # get current axes
ax2 = [Link]()
.twinx() method allows ax1 and ax2 to share the same x-axis.
Legend works only when you give label= inside plot()
[Link](fontsize=14)
merge() method
themes = pd.read_csv("[Link]”)
set_theme_count = [Link]({'id': set_theme_count.index, 'set_count':
set_theme_count.values})
merged_df = [Link](set_theme_count, themes, on='id')
Bar Chart
[Link](merged_df.name[:10], merged_df.set_count[:10])
[Link](fontsize=14, rotation=45)