Python Can';t从序列中删除小数点

Python Can';t从序列中删除小数点,python,pandas,replace,etl,data-cleaning,Python,Pandas,Replace,Etl,Data Cleaning,我正在尝试从熊猫系列中删除小数点。目前它是一个熊猫浮子。我将把它转换成字符串。然后我想删除小数点,这样我就可以使用此列合并其他数据集。这是代码的原始输出。“人口普查区编号”列是我试图更改的内容 State County Census Tract Number Life Expectancy 4737 California Los Angeles County, CA 1011.10 79

我正在尝试从熊猫系列中删除小数点。目前它是一个熊猫浮子。我将把它转换成字符串。然后我想删除小数点,这样我就可以使用此列合并其他数据集。这是代码的原始输出。“人口普查区编号”列是我试图更改的内容

State   County                              Census Tract Number    Life Expectancy
4737    California  Los Angeles County, CA  1011.10                 79.2
4738    California  Los Angeles County, CA  1011.22                 80.0
4739    California  Los Angeles County, CA  1012.10                 82.5
4740    California  Los Angeles County, CA  1012.20                 78.5
4741    California  Los Angeles County, CA  1013.00                 80.0
以下代码数据类型最初是浮点型,我将其转换为字符串:

df202['Census Tract Number'] = df202['Census Tract Number'].astype(str)
我出现以下错误,但仍能继续:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
这就是数据帧的变形:

4737    California  Los Angeles County, CA  1011.1  79.2
4738    California  Los Angeles County, CA  1011.22 80.0
4739    California  Los Angeles County, CA  1012.1  82.5
4740    California  Los Angeles County, CA  1012.2  78.5
4741    California  Los Angeles County, CA  1013.0  80.0
我不想让它在结尾处去掉零,但我继续并试图去掉小数:

df202['Census Tract Number'] = df202['Census Tract Number'].replace('.', '')
小数不删除


请帮忙

您可以使用一个小正则表达式来提取人口普查区编号:

df['Census Tract Number'] = df['Census Tract Number'].astype(str).str.extract('([0-9]{4})')
编辑: 仅替换点:

df['Census Tract Number'] = df['Census Tract Number'].astype(str).str.replace('.', '')

您可以使用小正则表达式提取人口普查区编号:

df['Census Tract Number'] = df['Census Tract Number'].astype(str).str.extract('([0-9]{4})')
编辑: 仅替换点:

df['Census Tract Number'] = df['Census Tract Number'].astype(str).str.replace('.', '')

您可以这样做:

df202['Census Tract Number'] = df202['Census Tract Number'].apply(lambda x : round(x))
print(df.head())
#State   County                              Census Tract Number    Life Expectancy
#4737    California  Los Angeles County, CA  1011                 79.2
#4738    California  Los Angeles County, CA  1011.22              80.0
#4739    California  Los Angeles County, CA  1012                 82.5
#4740    California  Los Angeles County, CA  1012                 78.5
#4741    California  Los Angeles County, CA  1013                 80.0

您可以这样做:

df202['Census Tract Number'] = df202['Census Tract Number'].apply(lambda x : round(x))
print(df.head())
#State   County                              Census Tract Number    Life Expectancy
#4737    California  Los Angeles County, CA  1011                 79.2
#4738    California  Los Angeles County, CA  1011.22              80.0
#4739    California  Los Angeles County, CA  1012                 82.5
#4740    California  Los Angeles County, CA  1012                 78.5
#4741    California  Los Angeles County, CA  1013                 80.0

嗨,谢谢你。不幸的是,这个答案去掉了小数点后的数字,我仍然需要这些数字,因为它们是我生活中的重要标识符data@ChuckEPryor嗨,我一定是误解了这个问题。看我的编辑。你好,谢谢。不幸的是,这个答案去掉了小数点后的数字,我仍然需要这些数字,因为它们是我生活中的重要标识符data@ChuckEPryor嗨,我一定是误解了这个问题。看到我的编辑。嗨,谢谢你…这个代码不执行。即使如此,我仍然需要小数点后的数字来保持机智。应该使用此代码,而不是您的测试。因此,请使用它而不是
df202['Census Tract Number']=df202['Census Tract Number'].astype(str)
。如果你仍然想要这个数字,那么你可以很容易地创建另一列。嗨,谢谢你…这段代码不执行。即使如此,我仍然需要小数点后的数字来保持机智。应该使用此代码,而不是您的测试。因此,请使用它而不是
df202['Census Tract Number']=df202['Census Tract Number'].astype(str)
。如果你仍然想要这个数字,那么你可以很容易地创建另一列。