简体   繁体   中英

How can I print only lines with a certain float value in a Pandas Dataframe

I do not have much experience with Python but I am working on a code where I need to manipulate data and delete/remove several hundred lines. I have my data in a pandas dataframe and there is one column where much of the data starts with the number 4.61 with random digits after the 1. I am trying to print only the lines that DO NOT have 4.61 at the beginning of this column. This is what I have so far,

remove_foursixone = df.loc[df[' Conductance (G0) '] != str.startswith('4.61')]

print(remove_foursixone)

but it won't work because str.startswith needs a string type but I am working with floats. I tried converting the data in the Conductance column to a string type using

df[' Conductance (G0) '] = df[' Conductance (G0) '].astype(str)

but this returns dtype('O') and still does not allow me to use the str.startswith() function. Any suggestions for the best way to approach this would be much appreciated.

Try this:

df[(df[col_name] < 4.61) | (df[col_name] >= 4.62)]

This way you exclude all values in range [4.61; 4.62[ [4.61; 4.62[ .

You can try:

remove_foursixone = df.loc[~df['Conductance (G0)'].astype(str).str.startswith('4.61')]

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