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
463 views
in Technique[技术] by (71.8m points)

python - 如何在python中剪切一个熊猫列,并使另一列的长度相等?(How to cut one pandas column in python, and make the other column equal in length?)

I have a dataframe consisting of a few columns, among these are X, Y and Z coordinates.

(我有一个由几列组成的数据框,其中包括X,Y和Z坐标。)

Now there columns are all of equal length.

(现在,所有列的长度相等。)

If you imagine a cylinder, what I am looking to do is to cut a part of the cylinder so that it gets shorter.

(如果您想象一个圆柱体,我想做的就是切割圆柱体的一部分,使其变得更短。)

I have managed to figure out how to cut column Z with respect to the actual values in column Z: cutted_z = [i for i in df["Z"] if i >= 0 and i <= 1000]

(我设法弄清楚如何相对于Z列中的实际值切割Z列: cutted_z = [i for i in df["Z"] if i >= 0 and i <= 1000])

How can one cut XYZ equally with respect to the actual numerical values in Z?

(如何相对于Z中的实际数值平等地切割XYZ?)

The problem with my current solution is that it leaves the the length of the X and Y columns, as well as the rest of the columns in my dataframe, untouched, meaning X and Y now have more columns in Z.

(我当前解决方案的问题是,它不影响X和Y列的长度以及数据框中其余列的长度,这意味着X和Y现在在Z中具有更多列。)

  ask by Zygos translate from so

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

1 Answer

0 votes
by (71.8m points)

You could just do:

(您可以这样做:)

mask = (df['Z'] >= 0) & (df['Z'] <= 1000) #creates a mask to filter dataframe on

df = df[mask]

This will give you a slice of dataframe for Z values range you wanted.

(这将为您提供所需的Z值范围的数据框。)


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

...