Python 熊猫得到年份过期栏
我有一个数据框,其中包含一个名为Python 熊猫得到年份过期栏,python,pandas,string,dataframe,Python,Pandas,String,Dataframe,我有一个数据框,其中包含一个名为start\u year的列 它里面写了很多风格,我只需要在这里写。 例如: 表中的起始年 按我的需要开始新的一年 1949年12月27日 1949 公元前168年 -168 不间断的 2021年(今天) 1902年(马尔瓦尔投降)1913年(不可调和活跃) 1902 1903-1905 1903 尝试通过extract()和fillna(): 如果-符号最初未出现在值为BC的'start\u year as I needed'列中,则您可以在运行上述代码后使用布
start\u year
的列
它里面写了很多风格,我只需要在这里写。
例如:
表中的起始年
按我的需要开始新的一年
1949年12月27日
1949
公元前168年
-168
不间断的
2021年(今天)
1902年(马尔瓦尔投降)1913年(不可调和活跃)
1902
1903-1905
1903
尝试通过extract()
和fillna()
:
如果-
符号最初未出现在值为BC的'start\u year as I needed'列中,则您可以在运行上述代码后使用布尔掩蔽:
m=df['start_year in table'].str.contains('BC')
df.loc[m,'start_year as I need']='-'+df.loc[m,'start_year as I need']
我在这里扮演了一个神奇的角色。。你能帮我吗?我希望公元前一年与前一年相同,比如:公元前168年->-168更新答案…请看一看:)谢谢!你能再帮我一点忙吗?对于日期为1949年12月27日的行,它保留了27值更新的答案……请看一看:)
m=df['start_year in table'].str.contains('BC')
df.loc[m,'start_year as I need']='-'+df.loc[m,'start_year as I need']