繁体   English   中英

带字符串的 Python Pandas Dataframe 插值

[英]Python Pandas Dataframe interpolation with strings

我想知道 Pandas Dataframe 是否也允许对字符串进行插值。 (我有值工作但不适用于字符串)。

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

所需输出:蓝色,蓝色,蓝色,蓝色,蓝色,红色

只需使用填充。

s = s.ffill()

不,您不能插入字符串,但是,可以将字符串转换为类别,然后对其进行插入。

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

在您的情况下, 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())

输出:

[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']

我相信以下内容也适用于字符串:

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

请参阅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