Python 在数据帧中放置str
我正在从csv加载df,该csv具有一些无限值(或者我猜是这样)。我不想更改csv文件(因为它们是我程序的输入) 因此,当我加载此特定文件时,如下所示:Python 在数据帧中放置str,python,string,pandas,dataframe,Python,String,Pandas,Dataframe,我正在从csv加载df,该csv具有一些无限值(或者我猜是这样)。我不想更改csv文件(因为它们是我程序的输入) 因此,当我加载此特定文件时,如下所示: blocked2 = pd.read_csv(file8, usecols=[1,2,3,4]) blocked2.columns = names1 blocked2.head(), blocked2.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 863708 e
blocked2 = pd.read_csv(file8, usecols=[1,2,3,4])
blocked2.columns = names1
blocked2.head(), blocked2.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 863708 entries, 0 to 863707
Data columns (total 4 columns):
Time 863708 non-null float64
LP 863708 non-null float64
HP 863708 non-null float64
Icomp 863708 non-null object
dtypes: float64(3), object(1)
memory usage: 26.4+ MB
这很奇怪,因为这是一列当前(ampères)值,所以它应该都是浮点数
查看csv文件,我发现某些值写为
24.12088000
∞
ˆž
因此,我查看了加载的数据帧中的这些值,得到
15907
15.908
9.569441
15.00891
24.12088000
15908
15.909
9.574703
15.02067
*∞*
15909
15.910
9.574703
15.03243
*∞*
15910
15.911
9.574703
15.02067
*∞*
我有无限的值,它们是str类型。我的问题是:我是否可以找到这些str值并从数据帧中删除它们?因为我会收到很多这些CSV文件,无限值不能在同一个位置
提前感谢您的帮助:)您可以执行以下操作 首先 然后 或
我自己还没有运行过这段代码,但是这些代码行中的某些内容应该可以工作。您可以通过将其转换为数字。然后使用和分别过滤非有限或非数字数据。下面是一个演示:
s = pd.Series([32.32, -np.inf, 'inf', 'asdfa', -324.42, np.inf])
s = pd.to_numeric(s, errors='coerce')
s = s[np.isfinite(s) & s.notnull()]
0 32.32
4 -324.42
dtype: float64
由于my infinite不是np.inf,因此此命令不会替换值。所以它不起作用:(
blocked2.replace([np.inf, -np.inf], np.nan,inplace=True)
blocked2.fillna(0,inplace=True)
blocked2.dropna(inplace=True)
s = pd.Series([32.32, -np.inf, 'inf', 'asdfa', -324.42, np.inf])
s = pd.to_numeric(s, errors='coerce')
s = s[np.isfinite(s) & s.notnull()]
0 32.32
4 -324.42
dtype: float64