简体   繁体   English

带字符串的 Python Pandas Dataframe 插值

[英]Python Pandas Dataframe interpolation with strings

I was wondering if Pandas Dataframe allows for interpolation for strings as well.我想知道 Pandas Dataframe 是否也允许对字符串进行插值。 (I have values working but not for strings). (我有值工作但不适用于字符串)。

 import pandas as pd import numpy as np s = pd.Series(["Blue", "Blue", np.nan, "Blue","Blue","Red"]) s = s.interpolate() print(s)
Output: Blue, Blue, NaN, Blue, Blue, Red 输出:蓝色,蓝色,NaN,蓝色,蓝色,红色

Desired Output: Blue, Blue, Blue, Blue, Blue, Red所需输出:蓝色,蓝色,蓝色,蓝色,蓝色,红色

只需使用填充。

s = s.ffill()

no, you can't interpolate strings, but, it is possible to convert the strings to categories and then interpolate on that.不,您不能插入字符串,但是,可以将字符串转换为类别,然后对其进行插入。

arr, cat = s.factorize()
s2 = pd.Series(arr).replace(-1, np.nan).interpolate()\
         .astype('category').cat.rename_categories(cat)\
         .astype('str')

In your case s.interpolate(method='pad') or s.ffill() will do just fine but you may compare and observe outputs of different techniques below:在您的情况下, s.interpolate(method='pad')s.ffill()会做得很好,但您可以比较和观察以下不同技术的输出:

import pandas as pd

s = pd.Series([None, None, 'red', 'red', None, 'blue', None, None])

print(s.to_list())
print(s.bfill().tolist())
print(s.ffill().tolist())
print(s.bfill().ffill().tolist())
print(s.ffill().bfill().tolist())
print(s.interpolate(method='pad').tolist())

Output:输出:

[None, None, 'red', 'red', None, 'blue', None, None]
['red', 'red', 'red', 'red', 'blue', 'blue', None, None]
[None, None, 'red', 'red', 'red', 'blue', 'blue', 'blue']
['red', 'red', 'red', 'red', 'blue', 'blue', 'blue', 'blue']
['red', 'red', 'red', 'red', 'red', 'blue', 'blue', 'blue']
[None, None, 'red', 'red', 'red', 'blue', 'blue', 'blue']

I believe that the following will also work for strings:我相信以下内容也适用于字符串:

s = s.interpolate(method='pad')

See the documentation at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.interpolate.html .请参阅https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.interpolate.html 上的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM