Python Csv file work


In this blog, we are going to learn " How to select row and columns at the same time in python from .csv file"


demo.csv




how to select any row from the above file

Step1: import and read .csv file
import pandas as pd
df = pd.read_csv('demo.csv')
print(df)


Output :



Step2: How to select any row in Python
The “iloc” in pandas is used to select rows and columns by number in the order that they appear in the DataFrame (df).
import pandas as pd
df = pd.read_csv('demo.csv')
print(df.loc[1])
# Here 1 represents the index number of a row

Output :

 


how to select any Column form the .csv file

Step1: import and read .csv file
import pandas as pd
df = pd.read_csv('demo.csv')
print(df)

Output :


Step2: How to select any column in Python
df = pd.read_csv('demo.csv')
print(df[['Age']])
print(df[['year']])

Output :


how to select a particular Column and row at the same time from the .csv file

Step1: import and read .csv file
import pandas as pd
df = pd.read_csv('demo.csv')
print(df)

Output :


Step2: How to select any column in Python
df = pd.read_csv("demo.csv")
 print(df["Name"][2])
print(df["Age"][2])

Output :






Comments