Categories
Python Answers

How to convert index of a Python Pandas dataframe into a column?

To convert index of a Python Pandas dataframe into a column, we cann reset_index.

For instance, we write

df = df.reset_index(level=0)

to call reset_index on the df data frame to convert index of a Python Pandas dataframe into a column

Categories
Python Answers

How to filter Python Pandas DataFrame by substring criteria?

To filter Python Pandas DataFrame by substring criteria, we can call the str.contains method.

For instance, we write

df[df['A'].str.contains("hello")]

to return a data frame with rows in column 'A' that has strings containing 'hello' with str.contains.

Categories
Python Answers

How to create an empty Python Pandas DataFrame, then filling it?

To create an empty Python Pandas DataFrame, then filling it, we can append the new data to a list and put the list data in the data frame.

For instance, we write

data = []
for a, b, c in some_function_that_yields_data():
    data.append([a, b, c])

df = pd.DataFrame(data, columns=['A', 'B', 'C'])

to call data.append to append the data in the data list.

Then we create the data frame from data using pd.DataFrame with data and set columns to an array with the column names.

Categories
Python Answers

How to merge Python Pandas data frames?

To merge Python Pandas data frames, we can call the merge method.

For instance, we write

np.random.seed(0)
left = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': np.random.randn(4)})
right = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': np.random.randn(4)})
m = left.merge(right, on='key')

to create the left and right dataframes with some random values.

Then we call left.merge with right and set on to 'key' to merge the rows by the key column value.

This will do an inner join.

We can also add the how argument to merge to do other kinds of joins.

So we can write

left.merge(right, on='key', how='left')

to set how to 'left' to do a left join.

We can also set how to 'right' or 'outer' to do those joins.

Categories
Python Answers

How to combine two columns of text in a Python Pandas dataframe?

To combine two columns of text in a Python Pandas dataframe, we get the values from the columns and combine them with operators.

For instance, we write

df["period"] = df["Year"] + df["quarter"]

to concatenate the 'Year' and 'quarter' values together to form the period column.

We can also convert columns to strings with astype by writing

df["period"] = df["Year"].astype(str) + df["quarter"]

before we do concatenation.