简体   繁体   English

第 3 列 pandas python 中至少有两列

[英]Minimum of two columns in a 3rd column pandas python

df df

Col1   Col2   Col3   Col4    Col5
3       1      22     13      0
5       4      10      12     14
1       2      1        1      1

and so on

Code代码

df['Col6'] = df.min(axis=1)

This will return minimum of all 5 columns but I want a column which returns minimum of 2 columns Minimum of col 4 and col 1 lets say in a 6th column.这将返回所有 5 列中的最小值,但我想要一列返回 2 列的最小值,列 4 和列 1 的最小值可以在第 6 列中说。 How can I get that我怎么能得到那个

Output expected : Min of col1 and col4 rounded off to 2 decimal places预期输出:col1 和 col4 的最小值四舍五入到小数点后两位

df[6] = round(min(df[col4], df[col1]),2) df[6] = round(min(df[col4], df[col1]),2)

You can filter both columns by list and then add Series.round :您可以按列表过滤两列,然后添加Series.round

df['Col6'] = df[['Col4','Col1']].min(axis=1).round(2)
print (df)
   Col1  Col2  Col3  Col4  Col5  Col6
0     3     1    22    13     0     3
1     5     4    10    12    14     5
2     1     2     1     1     1     1  

Your solution should working with numpy.minimum and numpy.round :您的解决方案应该与numpy.minimumnumpy.round

df['Col6'] = np.round(np.minimum(df['Col4'], df['Col1']),2) 

暂无
暂无

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

相关问题 Python Pandas - 检查两列中的值,对第三列求和 - Python Pandas - check value in two columns, sum the 3rd column 熊猫按两列分组,并从第三列输出值 - Pandas groupby two columns and output values from 3rd column 如何比较两列并获取 python pandas dataframe 中两列中所有匹配项的第三列的平均值? - how to compare two columns and get the mean value of the the 3rd column for all matching items in the two in python pandas dataframe? Python 比较 2 列并用第 3 列中的值写入第 4 列(熊猫) - Python Compare 2 Columns And Write A 4th Column With Values From 3rd Column (pandas ) 如何基于两列删除重复数据,从而删除熊猫数据框中第三列中最大的列? - How to remove duplicates based on two columns removing the the largest of 3rd column in pandas dataframe? 通过最小索引号对Pandas数据框组进行排序,然后基于第三列对组中的所有其他列进行重新排序 - Order Pandas dataframe groups by minimum index number, then re-order all other columns within groups based on a 3rd column Python Pandas:排序和分组,然后对第二列的两个连续行求和,得出第三列的特定值 - Python Pandas: Sort and group by, then sum two consecutive rows of 2nd column for a specific value of a 3rd column 在Pandas DataFrame中比较2列并填充第3列 - Comparing 2 columns in Pandas DataFrame and populating a 3rd column 如何使用其他两列作为轴 plot 第 3 列 - How to plot the 3rd column using the other two columns as axes Pandas:如果来自第三列的字符串值,则根据另一列的值创建列 - Pandas : Create columns based on values of another column if string value from 3rd column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM