在Python 3和pandas中,我有一个带有一列代码的数据帧。列" cnpj"
segura.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 35581 entries, 0 to 35580
Data columns (total 3 columns):
cnpj 35581 non-null object
nome_empresa 35581 non-null object
estado 35581 non-null object
dtypes: object(3)
memory usage: 834.0+ KB
此列始终有14位数字。示例:&#34; 00529528000152&#34;和&#34; 02197190000104&#34;
请问,我可以创建一个新列,其列的前八位数为&#34; cnpj&#34;在它的行?示例:&#34; 00529528&#34;和&#34; 02197190&#34;
答案 0 :(得分:2)
尝试:
segura['NEW_ID'] = segura['cnpj'].str[:8]