我有一个如下所示的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。我如何有效地做到这一点?
您要删除hyphen
还是删除由hyphen
联合的值?
mylist = new_dict.values()[0]
mylist.replace("acres","sqfeet").replace('-','')
'[1002, 322, 420500,300,1.25acres,100250,3.45 acres]'
尚不清楚这是否是家庭作业,您还没有向我们展示您根据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())