Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

python - What does 'range(y.shape[1])' mean in "for i in range(dataset2.shape[1]):"?

I'm trying to find out how this above-mentioned piece of code works in a layman sense? for context, this code contains Numpy, Seaborn, Pandas and matplotlib.

below is the line of code:

dataset2 = dataset.drop(columns = ['entry_id', 'pay_schedule', 'e_signed'])

fig = plt.figure(figsize=(15, 12))
plt.suptitle('Histograms of Numerical Columns', fontsize=20)

**for i in range(dataset2.shape[1]):**

    plt.subplot(6, 3, i + 1)

    f = plt.gca()

    f.set_title(dataset2.columns.values[i])
question from:https://stackoverflow.com/questions/65949568/what-does-rangey-shape1-mean-in-for-i-in-rangedataset2-shape1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

pd.shape will give you the number of rows and columns present in the dataFrame.

where, df.shape[0] will give you the total rows present in the dataFrame.

and, df.shape[1] will give you the number of columns present in the dataFrame.

Example:

df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011'], 
                    'Phrases':['I have a cool family', 'I like avocados', 'I would like to go to school']})


df
Out[26]: 
        Date                       Phrases
0  10/2/2011          I have a cool family
1  11/2/2011               I like avocados
2  12/2/2011  I would like to go to school

df.shape
Out[27]: (3, 2)

df.shape[0]   #number of rows
Out[28]: 3

df.shape[1]   #number of columns
Out[29]: 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...