Python 如何使用列中的值来确定要在不同数据帧中分析的列?
我有两个熊猫数据帧,一个是原始数据,另一个是基于第一个数据帧的数据分析的分析输出。设置如下:Python 如何使用列中的值来确定要在不同数据帧中分析的列?,python,pandas,dataframe,analysis,Python,Pandas,Dataframe,Analysis,我有两个熊猫数据帧,一个是原始数据,另一个是基于第一个数据帧的数据分析的分析输出。设置如下: df1 P1T P2T P3T P N P N P U P P U U U N df2 Indicator Indicator State Occurrences P1T P P1T N P1T U P2T P P2T N P2T U P3T P P3T
df1
P1T P2T P3T
P N P
N P U
P P U
U U N
df2
Indicator Indicator State Occurrences
P1T P
P1T N
P1T U
P2T P
P2T N
P2T U
P3T P
P3T N
P3T U
在df1
中,每列代表一个“指示器”,每个指示器可以有三种状态:“p”、“N”或“U”
df2
列出了每个“指示器”及其可能具有的状态范围,每个表示不同的情况。然后计算每个案例的出现次数,并在“出现次数”列中输出该数字。就是
df2
Indicator Indicator State Occurrences
P1T P 2
P1T N 1
P1T U 1
P2T P 2
P2T N 1
P2T U 1
P3T P 1
P3T N 1
P3T U 2
是否可以使用df2['Indicators']
列中的值来指定df1
中的列以执行计数,然后使用df2['Indicator State']
列中的值来提供“countif”条件?与多索引系列一起使用和用于多索引系列
:
df3 = (df1.melt(var_name='Indicator', value_name='Indicator State')
.groupby(['Indicator','Indicator State'])
.size()
.rename('Occurrences'))
另一个解决方案包括和:
最后将其保存到原始数据帧DataFrame
:
#if necessary remove only NaN column
df2 = df2.drop('Occurrences', axis=1)
df2 = df2.join(df3, on=['Indicator','Indicator State'])
print (df2)
Indicator Indicator State Occurrences
0 P1T P 2
1 P1T N 1
2 P1T U 1
3 P2T P 2
4 P2T N 1
5 P2T U 1
6 P3T P 1
7 P3T N 1
8 P3T U 2
非常感谢。这正是我一直在寻找的,工作起来很有魅力:)
print (df3)
Indicator Indicator State
P1T N 1
P 2
U 1
P2T N 1
P 2
U 1
P3T N 1
P 1
U 2
Name: Occurrences, dtype: int64
#if necessary remove only NaN column
df2 = df2.drop('Occurrences', axis=1)
df2 = df2.join(df3, on=['Indicator','Indicator State'])
print (df2)
Indicator Indicator State Occurrences
0 P1T P 2
1 P1T N 1
2 P1T U 1
3 P2T P 2
4 P2T N 1
5 P2T U 1
6 P3T P 1
7 P3T N 1
8 P3T U 2