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.

Categories
Python Answers

How to delete a DataFrame row in Python Pandas based on column value?

To delete a DataFrame row in Python Pandas based on column value, we can set get the rows we want with a condition.

For instance, we write

df = df[df.line_race != 0]

to get the rows where the line_race column isn’t 0

Categories
Python Answers

How to expand the output display to see more columns of a Python Pandas DataFrame?

To expand the output display to see more columns of a Python Pandas DataFrame, we call the set_option method.

For example, we write

import pandas as pd
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

to call set_option to set the values for max_rows, max_columns with width of each row.

Categories
Python Answers

How to write a Python Pandas DataFrame to CSV file?

To write a Python Pandas DataFrame to CSV file, we call the to_csv method on the data frame.

For instance, we write

df.to_csv(file_name, encoding='utf-8', index=False)

to call to_csv on the df data frame.

file_name is the path of the output file.

encoding sets the CSV encoding.

We hide the row index by setting index to false.