我有一个Dataframe
import pandas_datareader as webreader
import math
import numpy as np
import pandas as pd
from datetime import date, timedelta, datetime
from pandas.plotting import register_matplotlib_converters
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from sklearn.metrics import mean_absolute_error, mean_squared_error
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
from keras.callbacks import EarlyStopping
from sklearn.preprocessing import RobustScaler
stockname = 'INFOSYS'
symbol = 'INFY.NS'
df = webreader.DataReader(symbol, start=date_start, end=date_today, data_source="yahoo")
print(df)
输出为,
我想把“deb/eq”列添加到这个数据框中,并且我想添加特定于年份的值。
例如。
for all dates in 2020 - all deb/eq values to be 0.00
for all dates in 2019 - all deb/eq values to be 1.00
for all dates in 2018 and previous - all deb/eq values to be 2.00
如何基于dataframes year向dataframe添加新列和值?
1条答案
按热度按时间mwecs4sa1#
使用
Index.map
设置了不匹配的值(NaN
s) 至2.0
由Index.fillna
:与
numpy.select
:编辑: