У меня есть фрейм данных df
, и я хочу получить строки, в которых столбец Election
равен определенному году.
Unnamed: 0 Map Level Precinct ID Precinct Name Election Invalid Ballots (%) More Ballots Than Votes (#) More Votes Than Ballots (#) Total Voter Turnout (#) Total Voter Turnout (%) ... Average votes per minute (17:00-20:00) CDM ED FG GD LP NR UNM Results others
0 0 Precinct 1 63-1 2008 Parliamentary 0.0 0.0 0.0 749 62.11 ... 1.01 0.0 0.0 0.0 0.0 0.0 0.0 77.17 United National Movement 22.83
1 1 Precinct 10 63-10 2008 Parliamentary 0.0 0.0 0.0 419 70.42 ... 0.61 0.0 0.0 0.0 0.0 0.0 0.0 71.12 United National Movement 28.87
...
136 159 Precinct 8 63-1 2013 Presidential 1.75 0.0 0.0 506 50.75 ... 0.52 2.96 0.20 0.00 0.00 1.19 0.00 0.00 Giorgi Margvelashvili 95.65
137 160 Precinct 9 63-10 2013 Presidential 2.50 0.0 0.0 625 48.04 ... 0.66 1.92 0.80 0.00 0.00 1.60 0.00 0.00 Giorgi Margvelashvili 95.68
Допустим, я хочу выборы 2008 года. Поэтому я выполнил следующую функцию:
def results_precinct_election(precinct,election_year):
df['Election'] = df['Election'].astype(int)
df_election = df.loc[df['Election'] == election_year]
x = df_election[["Christian-Democratic Movement","European Democrats","Free Georgia","Georgian Dream","Labour Party","New Right","United National Movement","others"]]
Но я получил:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-203-317aa5d54b6d> in <module>
----> 1 x = results_precinct_election("63-1", 2008)
2 y = results_precinct_election("63-1", 2013)
3 random.seed(0)
4 beta = estimate_beta()
<ipython-input-202-1cd2d166f35a> in results_precinct_election(precinct, election)
1 # I want a line of a given precinct for a given election
2 def results_precinct_election(precinct,election):
----> 3 df['Election'] = df['Election'].astype(int)
4 df_election = df.loc[df['Election'] == election and df['Precinct Name'] == precinct]
5 x = df_election[["Christian-Democratic Movement","European Democrats","Free Georgia","Georgian Dream","Labour Party","New Right","United National Movement","others"]]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors, **kwargs)
5689 # else, only a single dtype is given
5690 new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5691 **kwargs)
5692 return self._constructor(new_data).__finalize__(self)
5693
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, **kwargs)
529
530 def astype(self, dtype, **kwargs):
--> 531 return self.apply('astype', dtype=dtype, **kwargs)
532
533 def convert(self, **kwargs):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
393 copy=align_copy)
394
--> 395 applied = getattr(b, f)(**kwargs)
396 result_blocks = _extend_blocks(applied, result_blocks)
397
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
532 def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
533 return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 534 **kwargs)
535
536 def _astype(self, dtype, copy=False, errors='raise', values=None,
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
631
632 # _astype_nansafe works fine with 1-d only
--> 633 values = astype_nansafe(values.ravel(), dtype, copy=True)
634
635 # TODO(extension)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
681 # work around NumPy brokenness, #1987
682 if np.issubdtype(dtype.type, np.integer):
--> 683 return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
684
685 # if we have a datetime/timedelta array of objects
pandas/_libs/lib.pyx in pandas._libs.lib.astype_intsafe()
ValueError: invalid literal for int() with base 10: '2008 Parliamentary - Majoritarian'