【发布时间】:2019-04-04 22:35:13
【问题描述】:
我正在尝试向 Panda 数据集添加一个新列。 这个新列 df['Year_Prod'] 派生自另一个 df['title'] 我从中提取年份。
数据示例:
country designation title
Italy Vulkà Bianco Nicosia 2013 Vulkà Bianco (Etna)
Portugal Avidagos Quinta dos Avidagos 2011 Avidagos Red (Douro)
代码:
import re
import pandas as pd
df=pd.read_csv(r'test.csv', index_col=0)
df['Year_Prod']=re.findall('\\d+', df['title'])
print(df.head(10))
我收到以下错误:
File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 3119, in __setitem__self._set_item(key, value)
File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 3194, in _set_item value = self._sanitize_column(key, value)
File "C:\Python37\lib\site-packages\pandas\core\frame.py", line 3391, in _sanitize_column value = _sanitize_index(value, self.index, copy=False)
File "C:\Python37\lib\site-packages\pandas\core\series.py", line 4001, in _sanitize_index raise ValueError('Length of values does not match length of ' 'index')
**ValueError: Length of values does not match length of index**
请告诉我您对此的看法,谢谢。
【问题讨论】:
-
您的标题中是否有多个数字?
-
@G.Anderson,好问题,我之前查过,每个标题只有一次出现。
标签: regex python-3.x pandas dataframe