我有大量的行和列的数据帧。我试图写一个循环,它将检查所有行的特定条件,当为true时,只返回我的第一个列名。我需要在新的Dataframe中存储列名和值
dataframe将如下所示:
col1 col2 col3 col6 col7 col8 col9
Name
John 0 23 0 1 4 1 23
victor 1 4 5 2 1 4 15
Alida 1 1 2 6 0 2 2
Natalie 0 1 1 4 2 3 4
Morman 3 3 1 0 5 2 1
我试图为每个大于或等于4的值获取第一个列名,结果将存储在Dataframe df2中,类似于
Name Greater Than 4
John col2
victor col2
Alida col6
Natalie col6
Morman col7
我是python新手。我正在尝试以下代码:
df2["Name"]=df1["Name"]
for index, row in df.iterrows():
if df.iloc[:,[index]] >=4:
df2["Greater Than 4"]=df.iloc[:,[index]]
data.csv:
代码:
输出:
如果至少有一个值大于或等于} 与^{} 一起使用:
4
,请将^{如果不确定,则可能存在不正确的输出添加^{} 和^{} 测试:
最后一个数据帧使用:
对于最大值和最大列名,可以使用:
相关问题 更多 >
编程相关推荐