Oh, the sea, all water

I’m sorry to see you pandas…… You’re going to have to work hard

There are Easter eggs at the end

Hierarchical/multilevel index

Let’s create a hierarchical index to see what happens

There are several ways to create this

Have a look at it

Created from from_tuples

import pandas as pd
import random

arrays = [['bar'.'bar'.'baz'.'baz'.'foo'.'foo'],
['one'.'two'.'one'.'two'.'one'.'two']]

tuples = list(zip(*arrays))

print(tuples)

index = pd.MultiIndex.from_tuples(tuples,names=['first'.'second'])
print(index)

s = pd.Series([3.1.4.1.5.9],index=index)

print(s)
Copy the code

We can pass in a list of tuples by calling pd.multiindex. from_tuples

It is also possible to pair each element in both iterations

The use of MultiIndex from_product


iterables = [['bar'.'baz'.'foo'], ['one'.'two']]
index = pd.MultiIndex.from_product(iterables,names=['first'.'second'])
print(index)

Copy the code

The effect is the same

In a more convenient, generate a multi-layer index

Pass the list directly to a Series or DataFrame to see it

arrays = [['bar'.'bar'.'baz'.'baz'.'foo'.'foo'],
          ['one'.'two'.'one'.'two'.'one'.'two']]

s = pd.Series([3.1.4.1.5.9],index=arrays)

print(s)

Copy the code

Look at this one-to-one correspondence

The latest 0.24 version is written for pandas

Added a method multiindex. from_frame

MultiIndex.from_frame(df, sortorder=None, names=None)

Use, relatively clear

df = pd.DataFrame([['A'.'nice'], ['A'.'to'],
                  ['B'.'good'], ['B'.'ye']],
                  columns=['first'.'second'])

print(df)

index = pd.MultiIndex.from_frame(df)
print(index)
Copy the code

Pandas is available for versions 0.24 and older

A small note

All MultiIndex constructors accept a names argument that stores the name of index itself, which defaults to None if not passed

Indexes can be set on any axis of the PANDAS object

In this case, just toss the chestnuts

data = [[1.2.4.5.6.7], [1.2.3.4.5.6]]
arrays = [['bar'.'bar'.'baz'.'baz'.'foo'.'foo'],
          ['one'.'two'.'one'.'two'.'one'.'two']]

tuples = list(zip(*arrays))

index = pd.MultiIndex.from_tuples(tuples,names=['first'.'second'])
df = pd.DataFrame(data,index=['A'.'B'],columns=index)
print(df)

Copy the code

Check the result and see that the columns name has been modified to multilevel

data = [[1.2.4.5], [1.2.3.4]]
arrays = [['bar'.'bar'.'baz'.'baz'.'foo'.'foo'],
          ['one'.'two'.'one'.'two'.'one'.'two']]

tuples = list(zip(*arrays))

index = pd.MultiIndex.from_tuples(tuples,names=['first'.'second'])
df = pd.DataFrame(data,index=['A'.'B'],columns=index[:4])
print(df)
Copy the code

Use index slices directly and select the number of indexes

Ok, so I’ll leave the multilevel index here

Continue tomorrow, Easter egg time

Welcome to follow the public number oh

t.cn/Ai9HgtwC

I have to hack a website today

The site is called Bo X Garden

The logic goes like this

At the time of release

Users can choose whether to upload a good home page, their choice

Then, the administrator deletes the “bad” ones

What’s a bad definition? That depends on how the permission dogs see it

Here we go. Here we go

Not only does it delete your posts, it also blocks your tweets

A word, you casually hair, I casually seal

rubbish

If you test the stress code in the future, throw it all in the garbage garden

Personal gripes, no rebuttals