pandas python -如何插入NaN值

qxsslcnc  于 2022-12-16  发布在  Python
关注(0)|答案(1)|浏览(176)

是否有办法在final_df中插入P & C的Nan值(其中str是步长相等的范围)?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df0 = pd.DataFrame({'str': [var for var in range(700,1260,5)]})
print(df0)

df1 = pd.DataFrame({'str': [700,705,710,715,720,1095,1100,1105,1110,1115,1120,1125,1130,1135,1205,1210,1215,1220,1225,1230,1235,1240,1245,1250,1255],
 'P': [0.075,0.075,0.075,0.075,0.075,17.95,19.75,21.85,24.25,26.55,29.2,31.9,35.05,37.7,98.6,102.15,108.5,113.5,118.4,123.55,127.3,132.7,138.7,142.7,148.35],
 'C': [407.8,403.65,398.3,391.65,387.8,30.05,26.65,23.7,21.35,19.65,16.05,14.3,11.95,9.95,0.475,0,0.525,0,0.2,0.175,0.15,0.375,0.125,0.075,0.175]})

df = pd.merge(df0,df1, on="str", how="outer")
print(df)
  • df.interpolate(方法=“三次”,限制方向=“both”)* 似乎不起作用...
dauxcl2d

dauxcl2d1#

我发现您的代码中缺少inplace=True。请将其插入到您的插值中以替换Nan值。

df.interpolate(method='cubic', limit_direction='both', inplace=True)

相关问题