Write a program in Python to store the city and state names that start with 'k' in a given DataFrame into a new CSV file

When working with pandas DataFrames, you often need to filter data based on specific criteria and save the results to a file. This example demonstrates how to filter cities and states that start with 'K' and export them to a CSV file.

Problem Statement

Given a DataFrame with City and State columns, we need to find rows where both the city name and state name start with 'K', then save these filtered results to a new CSV file.

Solution Approach

To solve this problem, we will follow these steps:

  • Create a DataFrame with city and state data

  • Filter rows where both City and State start with 'K' using str.startswith()

  • Save the filtered DataFrame to a CSV file using to_csv()

Example Implementation

Here's the complete implementation ?

import pandas as pd

# Create sample data
data = {
    'City': ['Chennai', 'Kochi', 'Kolkata', 'Mumbai', 'Kanpur'], 
    'State': ['Tamil Nadu', 'Kerala', 'West Bengal', 'Maharashtra', 'Uttar Pradesh']
}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# Filter cities and states that start with 'K'
filtered_df = df[df['City'].str.startswith('K') & df['State'].str.startswith('K')]
print("\nFiltered DataFrame (City and State start with 'K'):")
print(filtered_df)

# Save to CSV file
filtered_df.to_csv('cities_states_k.csv', index=False)
print("\nFiltered data saved to 'cities_states_k.csv'")
Original DataFrame:
      City         State
0  Chennai    Tamil Nadu
1    Kochi        Kerala
2  Kolkata   West Bengal
3   Mumbai  Maharashtra
4   Kanpur  Uttar Pradesh

Filtered DataFrame (City and State start with 'K'):
     City   State
1   Kochi  Kerala

Filtered data saved to 'cities_states_k.csv'

Key Methods Used

  • str.startswith('K') − Returns boolean series indicating which strings start with 'K'

  • & operator − Combines multiple boolean conditions

  • to_csv() − Exports DataFrame to CSV format

  • index=False − Prevents writing row indices to the CSV file

Alternative Approach

You can also filter using case-insensitive matching ?

import pandas as pd

data = {
    'City': ['chennai', 'kochi', 'Kolkata', 'mumbai'], 
    'State': ['tamil nadu', 'kerala', 'West Bengal', 'maharashtra']
}

df = pd.DataFrame(data)

# Case-insensitive filtering
filtered_df = df[df['City'].str.startswith('k', na=False) & 
                df['State'].str.startswith('k', na=False)]
print("Case-insensitive filtered data:")
print(filtered_df)
Case-insensitive filtered data:
    City    State
1  kochi   kerala

Conclusion

Use str.startswith() with boolean indexing to filter DataFrame rows based on string patterns. The to_csv() method provides a simple way to export filtered results to CSV files for further analysis.

---
Updated on: 2026-03-25T16:14:01+05:30

741 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements