15 things to do with just a single line of Pandas code
Pandas is a popular library for data manipulation and analysis in Python. Here are 15 things you can do with just a single line of Pandas code.
1. Read a CSV file
import pandas as pd; df = pd.read_csv("filename.csv")
2. Filter rows based on a condition
df[df['column'] > 5]
3. Sort a dataframe by a column
df.sort_values('column')
4. Group rows by a column and calculate the mean of another column:
df.groupby('column1')['column2'].mean()
5. Merge two dataframes
pd.merge(df1, df2, on='column')
6. Pivot a table
import pandas as pd; df.pivot(index='column1', columns='column2', values='column3')
7. Reshape a dataframe
df.melt(id_vars=['column1'], value_vars=['column2', 'column3'])
8. Rename columns
df.rename(columns={'old_column': 'new_column'})
9. Drop missing values
import pandas as pd; df.dropna()
10. Fill missing values with a specific value
df.fillna(value=0)
11. Calculate the correlation matrix
df.corr()
12. Calculate descriptive statistics
df.describe()
13. Apply a function to each element in a column
df['column'].apply(lambda x: x**2)
14. Create a new column based on a condition
df['new_column'] = np.where(df['column'] > 5, 'yes', 'no')
15. Write a dataframe to a CSV file
df.to_csv("filename.csv", index=False)
These are just a few examples of the many things you can do with a single line of Pandas code. With its powerful data manipulation and analysis capabilities, Pandas is a great library for working with structured data in Python.
Keep visiting Analytics Tuts for more tutorials.
Thanks for reading! Comment your suggestions and queries