A memorandum of how to write pandas that I tend to forget personally

Create a Sample DataFrame for work

df = pd.util.testing.makeMixedDataFrame()
df
A B C D
0 0.0 0.0 foo1 2009-01-01
1 1.0 1.0 foo2 2009-01-02
2 2.0 0.0 foo3 2009-01-05
3 3.0 1.0 foo4 2009-01-06
4 4.0 0.0 foo5 2009-01-07

Change pandas display options

pd.set_option('max_rows', 2)
pd.set_option('max_columns', 3)
df
A ... D
0 0.0 ... 2009-01-01
... ... ... ...
4 4.0 ... 2009-01-07

Change the type of multiple columns

df = df.astype({"A":"int64", "B":"int64"})
df.dtypes

A int64 B int64 C object D datetime64[ns] dtype: object

Change the format of multiple columns

df.assign(E=10000*df["A"])\
    .assign(F=100*df["B"])\
    .style.format(
    {
        "A":"{:.2f}", 
        "B":"{:.4f}", 
        "D":"{:%Y-%m-%d}",
        "E":"{:,}",
        "F":"{:}%"
    }
)
A B C D E F
0 0.00 0.0000 foo1 2009-01-01 0 0%
1 1.00 1.0000 foo2 2009-01-02 10,000 100%
2 2.00 0.0000 foo3 2009-01-05 20,000 0%
3 3.00 1.0000 foo4 2009-01-06 30,000 100%
4 4.00 0.0000 foo5 2009-01-07 40,000 0%

Do not set the key when groupby is index

df.groupby("B", as_index=False).agg({"A":"sum", "A":"mean"})
B A
0 0 2
1 1 2

Recommended Posts

A memorandum of how to write pandas that I tend to forget personally
How to write a list / dictionary type of Python3
[Python] How to write a docstring that conforms to PEP8
How to write a test for processing that uses BigQuery
I'll never forget how to write a shell script, don't forget! !!
How to write a metaclass that supports both python2 and python3
How to write a Python class
I tried to get a database of horse racing using Pandas
How to find the memory address of a Pandas dataframe value
[Python] Summary of how to use pandas
Qiita (1) How to write a code name
Summary of how to write AWS Lambda
[Python] How to read a csv file (read_csv method of pandas module)
How to create a wrapper that preserves the signature of the function to wrap
A memorandum of filter commands that you might forget in an instant
How to calculate the volatility of a brand
A simple example of how to use ArgumentParser
The story of IPv6 address that I want to keep at a minimum
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to write a ShellScript Bash for statement
How to remember when you forget a word
I want to create a web application that uses League of Legends data ①
I read "How to make a hacking lab"
How to write a named tuple document in 2020
[Go] How to write or call a function
How to make a Raspberry Pi that speaks the tweets of the specified user
I want to write to a file with Python
How to write a ShellScript bash case statement
A new form of app that works with GitHub: How to make GitHub Apps
I can't find the clocksource tsc! ?? The story of trying to write a kernel patch
The story of Linux that I want to teach myself half a year ago
How to create a property of relations that can be prefetch_related by specific conditions
The 15th offline real-time I tried to solve the problem of how to write with python
How to write a GUI using the maya command
A memo of how to use AIST supercomputer ABCI
I want to give a group_id to a pandas data frame
Syntax that Perl users tend to forget in Python
I want to write in Python! (2) Let's write a test
A memorandum on how to use keras.preprocessing.image in Keras
How to output CSV of multi-line header with pandas
I want to install a package of Php Redis
[Python] A memo to write CSV vertically with Pandas
A story that I was addicted to at np.where
I tried to summarize how to use pandas in python
Basics of PyTorch (2) -How to make a neural network-
How to use Pandas 2
How to start a simple WEB server that can execute cgi of php and python
How to write offline real time I tried to solve the problem of F02 with Python
[Python] I wrote a test of "Streamlit" that makes it easy to create visualization applications.
[Graph drawing] I tried to write a bar graph of multiple series with matplotlib and seaborn
[Python] How to make a list of character strings character by character
Offline real-time how to write Python implementation example of E14
How to shuffle a part of a Python list (at random.shuffle)
python Condition extraction from a list that I often forget
After all, how much should I write a Qiita article?
I want to start a lot of processes from python
How to enable Read / Write of net.Conn with context with golang
NikuGan ~ I want to see a lot of delicious meat! !!
I made a function to check the model of DCGAN
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]