如何删除熊猫数据框列中包含连字符的行?

问题描述 投票:1回答:2

我有一个如下所示的DataFrame:

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

df = pd.DataFrame([new_dict])
df.head()

我想删除连字符值,并将此数据帧中的英亩更改为sqfeet。我如何有效地做到这一点?

python-3.x pandas dataframe
2个回答
0
投票

您要删除hyphen还是删除由hyphen联合的值?

mylist = new_dict.values()[0]
mylist.replace("acres","sqfeet").replace('-','')

'[1002, 322, 420500,300,1.25acres,100250,3.45 acres]'


0
投票

尚不清楚这是否是家庭作业,您还没有向我们展示您根据https://stackoverflow.com/help/how-to-ask已经尝试过的内容>

这里可能会让您朝正确的方向前进:

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())
© www.soinside.com 2019 - 2024. All rights reserved.