只能比较标记相同的Series对象

问题描述 投票:0回答:1

我有以下公式:

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

但是,我一直收到错误消息:只能比较标记相同的Series对象

我正在尝试从dataframe'allhere'选择行,其中'RegionName'的值等于数据框'unitowns]中的值。

这些应该进入一个名为'universitytowns'的新数据框中。

同样,剩余的值在称为'Nonuniversitytowns'。的数据框中不相同。

我尝试对本系列中的值的顺序进行排序,但这似乎不起作用?

有人可以帮我吗?

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

数据框unitowns

看起来像这样:
State RegionName
443     Texas   Abilene 
349  Oklahoma       Ada 
327      Ohio       Ada 
194  Michigan    Adrian 
45   Colorado   Alamosa 

因此,universitytowns和nonuniversitytowns

应该具有same column作为dataframe'allhere'。

我有以下公式:allhere = allhere.sort_values('RegionName')unitowns = unitowns.sort_values('RegionName')universitytowns = allhere [allhere ['RegionName'] == unitowns ['RegionName']] ...

python pandas dataframe indexing series
1个回答
0
投票

您可以尝试以下方法:

© www.soinside.com 2019 - 2024. All rights reserved.