Categories
Python Answers

How to remove unwanted parts from strings in a column with Python Pandas?

To remove unwanted parts from strings in a column with Python Pandas, we can use the map method.

For instance, we write

data['result'] = data['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC'))

to call map with a lambda function that returns the original string with the unwanted parts removed with lstrip and rstrip.

And then we assign the returned results back to the result column

Categories
Python Answers

How to import CSV file as a Python Pandas DataFrame?

To import CSV file as a Python Pandas DataFrame, we can use the read_csv method.

For instance, we write

import pandas as pd
print pd.read_csv('value.txt')

to call read_csv with the csv file path to import the CSV and return a data frame from it.

Categories
Python Answers

How to loop through dataframes with Python Pandas?

Sometimes, we want to loop through dataframes with Python Pandas.

In this article, we’ll look at how to loop through dataframes with Python Pandas.

How to loop through dataframes with Python Pandas?

To loop through dataframes with Python Pandas, we use the iterator returned by the data frame iterrows method.

For instance, we write

for index, row in df.iterrows():
    # do something

to call df.iterrows to return an iterator from the df data frame.

And then we can do we what we want with each index and row.

Conclusion

To loop through dataframes with Python Pandas, we use the iterator returned by the data frame iterrows method.

Categories
Python Answers

How to get rows based on distinct values from one column with Python Pandas?

To get rows based on distinct values from one column with Python Pandas, we call the drop_duplicates method.

For instance, we write

df = df.drop_duplicates('COL2')

to call drop_duplicates with 'COL2' to drop the duplicate values from the COL2 column.

We can use the keep argument to keep the first or last values.

For instance, we write

df = df.drop_duplicates('COL2', keep='first')

to keep the first value with keep='first'.

And we use

df = df.drop_duplicates('COL2', keep='last')

to keep the lastvalue with keep='last'.

Categories
Python Answers

How to filter Python Pandas DataFrames on dates?

Sometimes, we want to filter Python Pandas DataFrames on dates.

In this article, we’ll look at how to filter Python Pandas DataFrames on dates.

How to filter Python Pandas DataFrames on dates?

To filter Python Pandas DataFrames on dates, we use loc with the date range.

For instance, we write

df.loc['2022-01-01':'2022-02-01']

to return the rows with dates between '2022-01-01' and '2022-02-01' with df.loc

Conclusion

To filter Python Pandas DataFrames on dates, we use loc with the date range.