【问题标题】:Move strings within a mixed string and float column to new column in Pandas将混合字符串和浮点列中的字符串移动到 Pandas 中的新列
【发布时间】:2018-08-16 18:47:16
【问题描述】:

似乎无法在任何地方找到答案。我的数据框中有一个“q”列,它既有字符串又有浮点数。我想从“q”中删除字符串值并将它们移动到现有的字符串列“cmets”中。任何帮助表示赞赏。

我试过了:

df['comments']=[isinstance(x, str) for x in df.q]

我也在 q 上尝试了一些 str 方法,但无济于事。对此的任何指导将不胜感激

【问题讨论】:

  • 您可以发布您的数据框示例吗?它们都是字符串,但您不想要数字吗?这些类型实际上是混合的吗?
  • s = pd.to_numeric(df.col1, errors='coerce') 为您提供转换后的浮点数列表。然后你有s.isnull().index 你将传输到cmets 的值的索引。您可以使用loc 做到这一点。如果您使用一些示例数据进行编辑,则更容易演示
  • 谢谢 RafaelC。你的回答肯定让我想到了正确的轨道。由于早期的连接,我的 df 索引存在一些问题,但是一旦我把它理顺,我就可以使用你的系列创建方法并使用 .loc 来解决它。 s = pd.to_numeric(df.q, errors='coerce') /n df['comments1'] = df.q.loc[s.isnull()]

标签: pandas


【解决方案1】:

如果系列是:

s=pd.Series([1.0,1.1,1.2,1.3,'this','is',1.4,'a',1.5,'comment'])
s
Out[24]: 
0          1
1        1.1
2        1.2
3        1.3
4       this
5         is
6        1.4
7          a
8        1.5
9    comment
dtype: object

那么只有浮点数可以是:

[e if type(e) is float else np.NaN for e in s if type(e)]
Out[25]: [1.0, 1.1, 1.2, 1.3, nan, nan, 1.4, nan, 1.5, nan]

而 cmets 可以是:

[e if type(e) is not float else '' for e in s if type(e)]
Out[26]: ['', '', '', '', 'this', 'is', '', 'a', '', 'comment']

这就是你想要做的。

但使用 pandas 的逐元素迭代不能很好地扩展,因此仅使用以下方法提取浮点数:

pd.to_numeric(s,errors='coerce')
Out[27]: 
0    1.0
1    1.1
2    1.2
3    1.3
4    NaN
5    NaN
6    1.4
7    NaN
8    1.5
9    NaN
dtype: float64

和:

pd.to_numeric(s,errors='coerce').to_frame('floats').merge(s.loc[pd.to_numeric(s,errors='coerce').isnull()].to_frame('comments'), left_index=True, right_index=True, how='outer')
Out[71]: 
   floats comments
0     1.0      NaN
1     1.1      NaN
2     1.2      NaN
3     1.3      NaN
4     NaN     this
5     NaN       is
6     1.4      NaN
7     NaN        a
8     1.5      NaN
9     NaN  comment

pd.to_numeric(s,errors='coerce') 有一个副作用,它将所有带有浮点文字的字符串转换为浮点而不是将其保留为字符串。

pd.to_numeric(pd.Series([1.0,1.1,1.2,1.3,'this','is',1.4,'a',1.5,'comment','12.345']), errors='coerce')
Out[73]: 
0      1.000
1      1.100
2      1.200
3      1.300
4        NaN
5        NaN
6      1.400
7        NaN
8      1.500
9        NaN
10    12.345   <--- this is now the float 12.345 not str
dtype: float64

【讨论】:

    【解决方案2】:

    如果您不想将带有浮点字面量的字符串转换为浮点数,也可以使用str.isnumeric() 方法:

    df = pd.DataFrame({'q':[1.5,2.5,3.5,'a', 'b', 5.1,'3.55','1.44']})
    
    df['comments'] = df.loc[df['q'].str.isnumeric()==False, 'q']
    
    In [4]: df
    Out[4]: 
          q comments
    0   1.5      NaN
    1   2.5      NaN
    2   3.5      NaN
    3     a        a
    4     b        b
    5   5.1      NaN
    6  3.55     3.55  <-- strings are not converted into floats
    7  1.44     1.44
    

    或者是这样的:

    criterion = df.q.apply(lambda x: isinstance(x,str))
    df['comments'] = df.loc[criterion, 'q']
    

    同样,它不会将字符串转换为浮点数。

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 2021-09-24
      • 2019-11-23
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      相关资源
      最近更新 更多