我正在搜索一个函数,它返回数据帧中元素的位置。 -数据帧中的值之间存在重复项 -数据帧约10*2000 -该函数将使用applymap()应用于数据帧
# initial dataframe
df = pandas.DataFrame({"R1": [8,2,3], "R2": [2,3,4], "R3": [-3,4,-1]})
Example:
get_position(2) is not clear as it could be either "R1" or "R2". I am wondering if there is another way that python knows which Position the element holds - possibly during the applymap() Operation
编辑:
你知道吗测向等级(轴=1,pct=真)
编辑2:
#intial dataframe
df_initial = pandas.DataFrame({"R1": [8,2,3], "R2": [2,3,4], "R3": [-3,4,-1]})
步骤1)
df_rank = df_initial.rank(axis=1,pct=True)
步骤2)
# Building Groups based on the percentage of the respective value
def function103(x):
if 0.0 <= x <= 0.1:
P1.append(get_column_name1(x))
return x
elif 0.1 < x <= 0.2:
P2.append(get_column_name1(x))
return x
elif 0.2 < x <= 0.3:
P3.append(get_column_name1(x))
return x
elif 0.3 < x <= 0.4:
P4.append(get_column_name1(x))
return x
elif 0.4 < x <= 0.5:
P5.append(get_column_name1(x))
return x
elif 0.5 < x <= 0.6:
P6.append(get_column_name1(x))
return x
elif 0.6 < x <= 0.7:
P7.append(get_column_name1(x))
return x
elif 0.7 < x <= 0.8:
P8.append(get_column_name1(x))
return x
elif 0.8 < x <= 0.9:
P9.append(get_column_name1(x))
return x
elif 0.9 < x <= 1.0:
P10.append(get_column_name1(x))
return x
else:
return x
步骤3)
# trying to get the columns Name of the the respective value
# my idea was to determine the Position of each value to then write a function
def get_column_name1(x)
#to return the values column Name
步骤4)
# apply the function
P1=[]
P2=[]
P3=[]
P4=[]
P5=[]
P6=[]
P7=[]
P8=[]
P9=[]
P10=[]
P11=[]
df_rank.applymap(function103).head()
如果需要数据帧中按值列出的索引或列名称,请使用^{} 作为位置,然后选择转换为numpy数组的所有索引或列值:
编辑:
或:
编辑:
函数的解决方案-需要通过重塑和提取创建的一列数据帧进行迭代系列名称,与列名相同:
相关问题 更多 >
编程相关推荐