
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Store City and State Names Starting with K in CSV using Python
Input −
Assume, we have DataFrame with City and State columns and find the city, state name startswith ‘k’ and store into another CSV file as shown below −
City,State Kochi,Kerala
Solution
To solve this, we will follow the steps given below.
Define a DataFrame
Check the city starts with ‘k’ as defined below,
df[df['City'].str.startswith('K') & df['State'].str.startswith('K')]
Finally, store the data in the ‘CSV’ file as below,
df1.to_csv(‘test.csv’)
Example
Let us see the following implementation to get a better understanding.
import pandas as pd import random as r data = { 'City': ['Chennai','Kochi','Kolkata'],'State': ['Tamilnad','Kerala','WestBengal']} df = pd.DataFrame(data) print("DataFrame is\n", df) df1 = df[df['City'].str.startswith('K') & df['State'].str.startswith('K')] df1.to_csv('test.csv')
Output
City,State Kochi,Kerala
Advertisements