Python sample code

You can sort a Pandas DataFrame using the sort_values() method

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'age': [25, 30, 35, 40, 45],
    'salary': [50000, 60000, 70000, 80000, 90000]
})

# Sort the DataFrame by 'age' column in ascending order
df_sorted = df.sort_values(by='age')

# Print the sorted DataFrame
print(df_sorted)

Sort in decending order

# Sort the DataFrame by 'age' column in descending order
df_sorted_desc = df.sort_values(by='age', ascending=False)

# Print the sorted DataFrame
print(df_sorted_desc)

sort by multiple columns by passing a list of column names to the by parameter:

# Sort the DataFrame by 'age' (in ascending order) and 'salary' (in descending order) columns
df_sorted_multi_desc = df.sort_values(by=['age', 'salary'], ascending=[True, False])

# Print the sorted DataFrame
print(df_sorted_multi_desc)

find the column name(s) in a Pandas DataFrame based on a specific condition, you can use the columns attribute along with a boolean indexing

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'age': [25, 30, 35, 40, 45],
    'salary': [50000, 60000, 70000, 80000, 90000]
})

# Find the columns where the values are greater than 35
cols = df.columns[df.gt(35).any()]

print(cols)

modify the condition inside the gt() method to suit your needs. For example, if you want to find columns where the values are equal to a specific value, you can use the eq() method instead of gt().

To count the number of people in the same country in Python, you can use Pandas DataFrame’s groupby() method along with the size() method. Here’s an example:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'age': [25, 30, 35, 40, 45],
    'country': ['USA', 'UK', 'USA', 'Canada', 'UK']
})

# Group the DataFrame by 'country' and count the number of occurrences in each group
grouped_df = df.groupby('country').size().reset_index(name='count')

print(grouped_df)

This code will count the number of people in the same country in the DataFrame. In this example, there are 2 people in the USA, 1 person in Canada, and 2 people in the UK. The output will be:

  country  count
0  Canada      1
1      UK      2
2     USA      2

Read csv into Pandas DataFrame

import pandas as pd
df = pd.read_csv('file.csv', dtype={'name':str, 'age': int, 'country':str}

print(df)