只能比较具有相同标签的系列对象

2024-06-01 04:17:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下公式:

allhere = allhere.sort_values('RegionName')
unitowns = unitowns.sort_values('RegionName') 
universitytowns = allhere[allhere['RegionName']==unitowns['RegionName']]
nonuniversitytowns = allhere[allhere['RegionName']!=unitowns['RegionName']]

但是,我一直收到错误:只能比较标签相同的系列对象

我试图从数据框“allhere”中选择行,其中'RegionName'的值等于数据框'unitowns'中的值

这些数据应该放在一个名为“universitytowns”的新数据框中

同样地,其余不相同的值也会被放入一个名为“UnuniversityTowns”的数据帧中。

我已经尝试过在这个系列中对值的顺序进行排序,但这似乎不起作用

谁能帮我一把吗

allhere的数据如下所示:

 State RegionName         2000q1         2000q2         2000q3  \
9171   Mississippi  Abbeville   82866.666667   84900.000000   85566.666667   
2062      Maryland   Aberdeen  115666.666667  117833.333333  119300.000000   
4445   Mississippi   Aberdeen   56733.333333   53266.666667   50233.333333   
6906         Texas  Abernathy            NaN            NaN            NaN   
11173        Texas   Abilene             NaN            NaN            NaN   

              2000q4         2001q1         2001q2         2001q3  \
9171    83266.666667   79833.333333   78933.333333   81933.333333   
2062   120666.666667  123766.666667  126100.000000  127266.666667   
4445    48533.333333   49633.333333   50933.333333   51400.000000   
6906             NaN            NaN            NaN            NaN   
11173            NaN            NaN            NaN            NaN   

              2001q4      ...              2014q3         2014q4  \
9171    85233.333333      ...       121966.666667  118700.000000   
2062   127133.333333      ...       200000.000000  199766.666667   
4445    50700.000000      ...        72133.333333   70000.000000   
6906             NaN      ...        65233.333333   64466.666667   
11173            NaN      ...                 NaN            NaN   

              2015q1         2015q2         2015q3         2015q4  \
9171   118466.666667  121200.000000  115566.666667  104633.333333   
2062   195266.666667  195266.666667  194433.333333  194800.000000   
4445    74633.333333   75200.000000   75900.000000   77000.000000   
6906    63933.333333   65300.000000   66166.666667   66800.000000   
11173            NaN            NaN            NaN            NaN   

              2016q1         2016q2    2016q3    difference  
9171   103966.666667  105233.333333  106500.0   2800.000000  
2062   196500.000000  198466.666667  198300.0  11066.666667  
4445    76200.000000   80733.333333   81150.0 -16100.000000  
6906    67433.333333   67066.666667   65750.0           NaN  
11173            NaN            NaN       NaN           NaN

数据帧单位拥有如下所示:

State RegionName
443     Texas   Abilene 
349  Oklahoma       Ada 
327      Ohio       Ada 
194  Michigan    Adrian 
45   Colorado   Alamosa 

因此,大学城和非大学城应具有与数据框“allhere”相同的列。


Tags: 数据nansortstatevaluesadatexasaberdeen
1条回答
网友
1楼 · 发布于 2024-06-01 04:17:18

您可以尝试以下方法:

allhere['unitown'] = allhere['RegionName'].isin(unitowns['RegionName'])

unitowns = allhere[allhere['unitown']==True]
nonunitowns = allhere[allhere['unitown']==False]
print(unitowns)
print(nonunitowns)


       State RegionName  2000q1  2000q2  2000q3  unitown
11173  Texas    Abilene     NaN     NaN     NaN     True

            State RegionName         2000q1         2000q2         2000q3  unitown
9171  Mississippi  Abbeville   82866.666667   84900.000000   85566.666667    False
2062     Maryland   Aberdeen  115666.666667  117833.333333  119300.000000    False
4445  Mississippi   Aberdeen   56733.333333   53266.666667   50233.333333    False
6906        Texas  Abernathy            NaN            NaN            NaN    False

相关问题 更多 >