Python 3.x 如何删除dataframe列中包含连字符的行?

Python 3.x 如何删除dataframe列中包含连字符的行?,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有一个数据框,如下所示: new_dict = {'Area_sqfeet': '[1002, 322, 420-500,300,1.25acres,100-250,3.45 acres]'} df = pd.DataFrame([new_dict]) df.head() 我想删除连字符值,并在此数据框中将acres更改为sqfeet。 我如何才能有效地完成它?使用列表理解: 步骤1:删除连字符 filtered_list = [i for i in mylist if "-" not

我有一个数据框,如下所示:

new_dict = {'Area_sqfeet': '[1002, 322, 420-500,300,1.25acres,100-250,3.45 acres]'}

df = pd.DataFrame([new_dict])
df.head()
我想删除连字符值,并在此数据框中将acres更改为sqfeet。 我如何才能有效地完成它?

使用列表理解: 步骤1:删除连字符

filtered_list  = [i for i in mylist if "-" not in i] # remove hyphens
步骤2:将英亩转换为平方英尺

final_list = [i if 'acres' not in i else eval(i.split('acres')[0])*43560 for i in filtered_list] # convert to sq foot

#['1002', '322', '300', 54450.0, 150282.0]
此外,如果您想保留转换值旁边的“sqfeet”,请使用以下命令:

final_list = [i if 'acres' not in i else "{} sqfeet".format(eval(i.split('acres')[0])*43560) for i in filtered_list]

# ['1002', '322', '300', '54450.0 sqfeet', '150282.0 sqfeet']
使用列表理解: 步骤1:删除连字符

filtered_list  = [i for i in mylist if "-" not in i] # remove hyphens
步骤2:将英亩转换为平方英尺

final_list = [i if 'acres' not in i else eval(i.split('acres')[0])*43560 for i in filtered_list] # convert to sq foot

#['1002', '322', '300', 54450.0, 150282.0]
此外,如果您想保留转换值旁边的“sqfeet”,请使用以下命令:

final_list = [i if 'acres' not in i else "{} sqfeet".format(eval(i.split('acres')[0])*43560) for i in filtered_list]

# ['1002', '322', '300', '54450.0 sqfeet', '150282.0 sqfeet']

现在还不清楚这是否是家庭作业,而且你还没有向我们展示你已经尝试过的内容

以下是一些可能会让你走上正确方向的东西:

import pandas as pd

col_name = 'Area_sqfeet'

# per comment on your question, you need to make a dataframe with more
# than one row, your original question only had one row
new_list = ["1002", "322", "420-500","300","1.25acres","100-250","3.45 acres"]

df = pd.DataFrame(new_list)
df.columns = ["Area_sqfeet"]

# once you have the df as strings, here's how to remove the ones with hyphens
df = df[df["Area_sqfeet"].str.contains("-")==False]
print(df.head())

现在还不清楚这是否是家庭作业,而且你还没有向我们展示你已经尝试过的内容

以下是一些可能会让你走上正确方向的东西:

import pandas as pd

col_name = 'Area_sqfeet'

# per comment on your question, you need to make a dataframe with more
# than one row, your original question only had one row
new_list = ["1002", "322", "420-500","300","1.25acres","100-250","3.45 acres"]

df = pd.DataFrame(new_list)
df.columns = ["Area_sqfeet"]

# once you have the df as strings, here's how to remove the ones with hyphens
df = df[df["Area_sqfeet"].str.contains("-")==False]
print(df.head())

在您的100-250示例中,您的意思是完全删除该值(以及420-500),比如删除它们?只是想澄清一下,您不仅仅是在尝试删除连字符,还创建了一个具有单行的数据帧!问得好。列表理解是这里的赢家。看我的回答在你100-250的例子中,你的意思是完全删除该值(连同420-500),就像删除它们一样?只是想澄清一下,您不仅仅是在尝试删除连字符,还创建了一个具有单行的数据帧!问得好。列表理解是这里的赢家。查看我的回答我不想只删除连字符,而是完全删除该值。要将1.25英亩转换为54450(单位转换平方英尺),我不想只删除连字符,而是完全删除该值。并且想要将1.25英亩转换为54450(单位转换平方英尺),想要将1.25英亩转换为54450(单位转换平方英尺),想要将1.25英亩转换为54450(单位转换平方英尺)