我有一个如下所示的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。 我怎样才能有效地做到这一点?
答案 0 :(得分:1)
mylist = ["1002", "322", "420-500","300","1.25acres","100-250","3.45 acres"]
# ['1002', '322', '420-500', '300', '1.25acres', '100-250', '3.45 acres']
第1步:删除连字符
filtered_list = [i for i in mylist if "-" not in i] # remove hyphens
第2步:将英亩转换为sqfeet :
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 :(得分:-1)
目前尚不清楚这是否是家庭作业,您还没有向我们展示您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())