Selecting Specific Rows of CSV Based on a Column'S Value in Python

Python Select Specific Row and Column

When you think CSV, think pandas.

import pandas as pd

df = pd.read_csv('path/to/csv')

if df.iloc[5, 6]:
# do stuff
else
# do some other stuff

Write specifics rows with specific column values in python csv

As I understand your problem, you want to read csv file, then based on a condition on any column's value you want to filter it and finally write into a csv file.
If I am correct then you can do the following :

#Import pandas

import pandas as pd

#Read your csv file as pandas dataframe
df = pd.read_csv("your_csv_file_name")

#Apply filter condition
df = df[df['Column_name_for_fitering'] == "Value_for_filtering"]

# Save as new csv file
df.to_csv('your_output_file_name')

Returning rows in CSV based on column match to values in other CSV

If you are trying to filter rows based on the item_list.csv you could try:

import pandas as pd
import numpy as np
from pandas import DataFrame

df = pd.read_csv(r"usage_data.csv", encoding='latin-1')
df_items = pd.read_csv(r"item_list.csv", encoding='latin-1', names=['DOI'])
article_usage = df.loc[df['DOI'] .isin(df_items['DOI'])]
article_usage.to_csv(r"article_usage.csv", index=False)

This assumes item_list.csv has no header and is just a list of required DOI entries.

Select csv rows with unique value in a particular column Python

You can create an empty set and add the values of the first columns to it. If it's already in the set, just skip to the next row, eg:

import csv

column_values = set()
new_rows = []

with open('example.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if (row[0] in column_values):
continue
column_values.add(row[0])
new_rows.append(row)

with open('updated.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(new_rows)

select rows from a DataFrame based on column value, limit to 16384 rows

Try

selected_row = data.loc[data['CHR'].isin([1, '1'])]


Related Topics



Leave a reply



Submit