Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- df = pd.DataFrame({
- 'A': [10, 20, 30],
- 'B': [1, 2, 3]
- })
- print(df)
- -----------------------------
- df = pd.DataFrame({
- 'A': [10, 20, 30]})
- df['A_squared'] = df['A'].apply(lambda x: x**2)
- print(df['A_squared'])
- --------------------------------
- # Sum of A and B row-wise
- df = pd.DataFrame({
- 'A': [10, 20, 30],
- 'B': [1, 2, 3]
- })
- df['Row_Sum'] = df.apply(lambda row: row['A'] + row['B'], axis=1)
- print(df['Row_Sum'])
- -------------------------------
- df_csv = pd.read_csv('data.csv')
- print(df_csv)
- -------------------------------
- df_excel = pd.read_excel('data.xlsx')
- -------------------------------
- df_json = pd.read_json('data.json')
- -------------------------------
- import sqlite3
- conn = sqlite3.connect('data.db')
- df_sql = pd.read_sql_query("SELECT * FROM my_table", conn)
- ---------------------------------
- import pandas as pd
- df = pd.DataFrame({
- 'ID': [1, 2, 4],
- 'Marks': [85, 90, 78]
- })
- df.describe() # Summary statistics
- -------------------------------
- df.mean()
- -------------------------------
- df.median() # Median
- -------------------------------
- df.std() # Standard deviation
- -------------------------------
- df.var() # Variance
- -------------------------------
- df.min(), df.max() # Min, Max
- -------------------------------
- df.corr() # Correlation matrix
- -------------------------------
- import pandas as pd
- # Sample DataFrame with Category
- df3 = pd.DataFrame({
- 'Item': ['Apple', 'Banana', 'Carrot',
- 'Banana','Apple', 'Carrot'],
- 'Category': ['Fruit', 'Fruit', 'Vegetable',
- 'Fruit', 'Fruit', 'Vegetable'],
- 'Quantity': [10, 5, 8, 6, 7, 3],
- 'Price': [15, 10, 12, 10, 15, 12]
- })
- print(df3)
- -------------------------------
- grouped = df3.groupby('Category').sum()
- print(grouped)
- -------------------------------
- df1 = pd.DataFrame({
- 'ID': [1, 2, 3],
- 'Name': ['Alice', 'Bob', 'Charlie']
- })
- df2 = pd.DataFrame({
- 'ID': [1, 2, 4],
- 'Marks': [85, 90, 78]
- })
- df_combined = pd.concat([df1, df2], axis=0) # Row-wise
- print(df_combined)
- df_merged = pd.concat([df1, df2], axis=1) # Column-wise
- print(df_merged)
- -------------------------------
Advertisement
Add Comment
Please, Sign In to add comment