当在数据框构造函数中使用’squeeze’关键字时为什么会出错?

huangapple go评论260阅读模式
英文:

Why am I getting error when using 'squeeze' keyword in dataframe constructor?

问题

使用关键字 squeeze 将一个单列数据框转换为 Series 时,出现错误 TypeError: read_csv() got an unexpected keyword argument 'squeeze'

期望得到一个 Series。

英文:
alcohol = pd.read_csv('https://andybek.com/pandas-drinks', usecols=['country','wine_servings'], index_col='country', squeeze=True)

when using the keyword squeeze to convert a one columned dataframe to series getting an error TypeError: read_csv() got an unexpected keyword argument 'squeeze'.

expecting a series

答案1

得分: 1

read_csv不再有squeeze参数。它在Pandas 2.0中被移除。

read_csv()中删除了参数..., squeeze, ... (..., GH43427)。

在读取CSV后,改用DataFrame.squeeze('columns')

英文:

read_csv no longer has a squeeze parameter. It was removed in Pandas 2.0.

> - Removed arguments ..., squeeze, ... from [read_csv()](https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html#pandas.read_csv "pandas.read_csv") (..., GH43427)

Instead use DataFrame.squeeze('columns') after reading the CSV.

答案2

得分: 0

read_csv()函数中的squeeze参数在pandas库中不可用。squeeze参数在其他pandas函数中使用,例如DataFrame.squeeze()Series.squeeze(),用于将具有单个列或轴的DataFrame或Series转换为Series。

import pandas as pd

alcohol = pd.read_csv('https://andybek.com/pandas-drinks', usecols=['country', 'wine_servings'], index_col='country')
alcohol = alcohol['wine_servings'].squeeze()
英文:

The squeeze parameter in the read_csv() function is not available in the pandas library. The squeeze parameter is used in other pandas functions such as DataFrame.squeeze() or Series.squeeze() to convert a DataFrame or Series with a single column or axis into a Series.

import pandas as pd

alcohol = pd.read_csv('https://andybek.com/pandas-drinks', usecols=['country', 'wine_servings'], index_col='country')
alcohol = alcohol['wine_servings'].squeeze()

huangapple
  • 本文由 发表于 2023年7月14日 09:17:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76684141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定