Skip to content

Latest commit

 

History

History
60 lines (39 loc) · 1.51 KB

File metadata and controls

60 lines (39 loc) · 1.51 KB

Pandas

Rename csv header

We can use rename function to change the header.

For example, I want to change column A to B, column C to D.

Syntax:

df.rename(columns={'A':'B','C':'D'}, inplace=True)

For more explanation, you can refer here

Wordcloud

When you did word-clouds project in python, you may see the key error as below:

command'/usr/bin/clang' failed with exit status 1

You can add one line of code showed here xcode-select --install, then you can solve the problem.

Check column type and convert column type to numeric

A very typical example when performing numeric compuations in pands, e.g. calculating correlation:

df['Price'].corr(df['Num of equipment'])

One may encounter the following error:

     76     if isinstance(ret, mu.ndarray):
     77         ret = um.true_divide(
---> 78                 ret, rcount, out=ret, casting='unsafe', subok=False)
     79         if is_float16_result and out is None:
     80             ret = arr.dtype.type(ret)

TypeError: unsupported operand type(s) for /: 'str' and 'int'

This hints that the data type is incorrect. We can check the type as follows:

df_sort['Price'].dtype
# dtype('O')

Convert the column to numeric in following way:

df_sort['p'] = pd.to_numeric(df_sort['Price'])
df_sort['p'].dtype
# dtype('int64')

The dtype('int64') means that this columns is good for numeric computation now.