Python 如何更改两列中两个随机数的null值(在这些列的最小值和最大值之间)?
我在pandas中的数据集中有以下列:Python 如何更改两列中两个随机数的null值(在这些列的最小值和最大值之间)?,python,python-3.x,pandas,anaconda,Python,Python 3.x,Pandas,Anaconda,我在pandas中的数据集中有以下列: Index(['Country', 'Region', 'Happiness Rank', 'Happiness Score', 'Lower Confidence Interval', 'Upper Confidence Interval', 'Economy (GDP per Capita)', 'Family', 'Health (Life Expectancy)', 'Freedom', 'Trust (G
Index(['Country', 'Region', 'Happiness Rank', 'Happiness Score',
'Lower Confidence Interval', 'Upper Confidence Interval',
'Economy (GDP per Capita)', 'Family', 'Health (Life Expectancy)',
'Freedom', 'Trust (Government Corruption)', 'Generosity',
'Dystopia Residual'],
dtype='object')
我需要将“下置信区间”和“上置信区间”列中的空值更改为每列最小值和最大值之间的随机数。两列中的值都是带小数的整数
这就是我尝试过的:
import random
print(random.randint((df.max(axis=0)["Lower Confidence Interval"]),(df.mmin(axis=0)["Lower Confidence Interval"])(df.max(axis=0)["Upper Confidence Interval"]),(df.mmin(axis=0)["Upper Confidence Interval"])
df.loc[:, ["Lower Confidence Interval", "Upper Confidence Interval"]].fillna(5, inplace=True)
这是我收到的错误消息:
File "<ipython-input-100-e3190b8f67a4>", line 1
print(random.randint((df.max(axis=0)["Lower Confidence Interval"]),(df.mmin(axis=0)["Lower Confidence Interval"])(df.max(axis=0)["Upper Confidence Interval"]),(df.mmin(axis=0)["Upper Confidence Interval"])
SyntaxError: unexpected EOF while parsing
文件“”,第1行
打印(random.randint((df.max(axis=0)[“下置信区间]),(df.mmin(axis=0)[“下置信区间])(df.max(axis=0)[“上置信区间]),(df.mmin(axis=0)[“上置信区间])
SyntaxError:分析时出现意外的EOF
我被困在这里有一段时间了,无法克服这个错误。有什么线索吗
提前感谢!:)让我们使用这个假设数据集,
df
:
sample_col
0 nan
1 nan
2 4.41
3 9.79
4 8.24
5 7.04
6 4.41
7 4.09
8 5.58
9 6.34
您可以创建名为use\u min和use\u max的int
对象,这些对象将从当前列的min()
和max()
值派生
use_min , use_max = int(df['sample_col'].min()) , int(df['sample_col'].max())
然后,您可以使用random.randint
(允许您生成随机数)来fillna
,它将最小值和最大值作为参数,这可以是您的use\u min,use\u max
import random
df['sample_col'].fillna(random.randint(use_min,use_max))
Out[342]:
0 6.00
1 6.00
2 4.41
3 9.79
4 8.24
5 7.04
6 4.41
7 4.09
8 5.58
9 6.34
Name: sample_col, dtype: float64
检查你的括号谢谢!这可以避免错误,但它不会让我得到最小值和最大值之间的随机数。我试图避免的是必须选择我要手动填写的名称。感谢澄清。我提供了一个新的答案。希望这能让你得到你需要的。确实如此!非常感谢你的快速帮助!