在Pandas中删除NaN的问题

2024-03-28 22:53:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了很长时间将pandas中数据集中的一列从“object”更改为“int64”。我的数据帧名为bsblandings。你知道吗

我的bsblandings.info文件()输出如下所示:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 810 entries, 0 to 809
Data columns (total 9 columns):
Year           810 non-null int64
Coast          810 non-null object
Subregion      810 non-null object
State          810 non-null object
Common Name    810 non-null object
Pounds         810 non-null object
Live Pounds    810 non-null object
Dollars        810 non-null object
% Display      810 non-null object
dtypes: int64(1), object(8)
memory usage: 57.0+ KB

我需要使用“磅”列,我成功地将所有非int64值从“*”更改为“0”。我也试过用numpy和NaN。你知道吗

我用过:

bsblandings = bsblandings.replace('*', ' ')

这并没有将数据类型从“object”更改为“int64”(尽管所有“*”实际上都被“0”替换)。你知道吗

然后,我尝试使用以下方法对磅列进行排序:

bsblandings.sort_values("Pounds")

我真正需要的是将磅列从最小到最大(或从最大到最小)排序。当我尝试使用.sort\值进行此操作时,它没有正确地对列进行排序。相反,我得到了一个输出订单103800,10400,104400,10600:

90  1951    US Atlantic Coast   North Atlantic  MASSACHUSETTS   BASS, BLACK SEA 103800  103800      100%
223 1964    US Atlantic Coast   North Atlantic  MASSACHUSETTS   BASS, BLACK SEA 10400   10400   1687    100%
380 1977    US Atlantic Coast   North Atlantic  MASSACHUSETTS   BASS, BLACK SEA 104400  104400  67172   100%
269 1965    US Atlantic Coast   North Atlantic  MASSACHUSETTS   BASS, BLACK SEA 10600   10600   1379    100%

我是个笨蛋,我找了又找,但我总是碰壁。任何帮助都将不胜感激。你知道吗


Tags: object排序nullusblacknonnorthsea
2条回答

这不是错误:排序是正确的。您的Pounds列是字符串格式的,因此这就是应用的排序。字符串是按排序顺序排序的,而不是明显的数值。因此,以“103”开头的任何值都小于以“104”开头的任何值。你知道吗

如果需要数字排序,请将列转换为int,或者指定一个排序键,该键在执行时强制转换为int。你知道吗

这就搞定了!你知道吗

bBlandings[“磅”]=pd.to\数字(bBlandings[“磅”])

谢谢!你知道吗

相关问题 更多 >