Categories
Python Answers

How to determine whether a Python Pandas Column contains a particular value?

To determine whether a Python Pandas Column contains a particular value, we can use the in operator.

For instance, we write

s = pd.Series(list('abc'))
1 in s

to create a series with 'a', 'b', and 'c' as its values.

And then we can check if 1 is in the series s with 1 in s.

Categories
Python Answers

How to create Python Pandas DataFrame from txt file with a specific pattern?

To create Python Pandas DataFrame from txt file with a specific pattern, we can use the read_csv method.

For instance, we write

df = pd.read_csv('filename.txt', sep=";", names=['Region Name'])

to call read_csv with the path of the txt file to read from.

sep is set to the separator for the row items.

And names is the column name of the columns to create.

Categories
Python Answers

How to find the column name which has the maximum value for each row with Python Pandas?

To find the column name which has the maximum value for each row with Python Pandas, we can use the idxmax method.

For instance, we write

df.idxmax(axis=1)

to call idxmax with axis set to 1 to return the column with the great value on each row.

Categories
Python Answers

How to sum DataFrame rows for given columns with Python Pandas?

To sum DataFrame rows for given columns with Python Pandas, we can use the sum method.

For instance, we write

df = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['dd','ee','ff'], 'd':[5,9,1]})
df['e'] = df.sum(axis=1)

to call df.sum with axis set to 1 to sum the row values.

It’ll also ignore non numeric columns when doing the sum.

Categories
Python Answers

How to make a copy of a data frame in Python Pandas?

To make a copy of a data frame in Python Pandas, we can use the copy method.

For instance, we write

df2 = df.copy()

to make a copy of the df data frame with copy and assign the copied data frame to df2.