简体   繁体   中英

how can I make range with text and make this to y-axis in plot

I have two questions!

Q1. first I want to make a list like

df.CellNo = [Cell1,Cell2,Cell3,...,Cell96]

so I tried df.CellNo = ['CellNo'] + range(1,97) but it was not working.

so I did a code like behind.

df = pd.DataFrame({'CellNo': range(1,97)})
df.CellNo = df.CellNo.astype(str)
df.CellNo = 'Cell' + df.CellNo

Is there any easier way to do this?? (in short way)

Q2. second I want to plot with x-axis is 'Time' data and y-axis is df.CellNo data when I just plot df

two muppets I want to make y-axis sorted by nuber like Cell5, Cell6, Cell7, Cell10, Cell11 ... from behind. how can I do,,

IIUC, this is about sorting dataframe by substring.

You can make a key column and then sort the dataframe:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Time' : [20190721,20190722,20190723], 'No':['A90','A5','A10']})
df['key'] = df['No'].str.extract('(\d+)').astype(int)
df.sort_values('key', inplace=True)
plt.scatter(df.Time,df.No)

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM