[英]Pandas Fill column with date
I am trying to fill a DataFrame column(entire column) in with a static date but which has to be a variable. 我试图用一个静态日期填充一个DataFrame列(整个列),但必须是一个变量。 I am able to fill in numbers using numpy, but I am unable to fill in a date for a column:
我可以使用numpy填写数字,但无法填写列的日期:
currdate = ['3/31/2018'] #this is what I need filled in
missing = ['GREEN', 'YELLOW', 'RED', 'BLUE', 'PINK']
Section 1: 第1节:
A = ({'PERIOD DT': pd.to_datetime(currdate),
'TEAM': missing,
'REGION': ['NA'],
'SECTOR': ['OTHER'],
'REVENUE': [0]})
Can anyone explain why the above code does not fill down in each respective column? 谁能解释为什么上面的代码没有在相应的栏中填写? For each column with the exception of 'TEAM', the values only fill in the first row of data within the DataFrame.
对于除“ TEAM”外的每一列,这些值仅填充DataFrame中数据的第一行。
DF = pd.DataFrame.from_dict(A, orient='index')
DF= DF.transpose()
Section 2: 第2节:
zeroVal = [0]
n = len(DF)
DF['SECTOR'] = 'OTHER'
DF['REGION'] = 'NA'
DF.loc[:,'REVENUE'] = np.zeros(n)
I tried using the below to fill in the 'currdate' I am using in the 'PERIOD DT' column, but I am unable to get it to work: 我尝试使用以下代码在“ PERIOD DT”列中填写正在使用的“日期”,但无法正常使用:
DF['PERIOD DT'] = pd.to_datetime(currdate)
Without telling each column what to do in section 2, I get the following output: I would like all of the values in the columns to be the same filled down each column with the exception of the TEAM that i fill in using a variable referencing a list 在不告诉第2节中每个列做什么的情况下,我得到以下输出:我希望列中的所有值在每个列中都相同,但TEAM除外,我使用引用变量的变量来填充清单
PERIOD DT TEAM REGION SECTOR REVENUE
0 2018-03-31 GREEN NA OTHER 0.00
1 NONE YELLOW NONE NONE NONE
2 NONE RED NONE NONE NONE
3 NONE BLUE NONE NONE NONE
4 NONE PINK NONE NONE NONE
My intended output: 我的预期输出:
PERIOD DT TEAM REGION SECTOR REVENUE
0 2018-03-31 GREEN NA OTHER 0.00
1 2018-03-31 YELLOW NA OTHER 0.00
2 2018-03-31 RED NA OTHER 0.00
3 2018-03-31 BLUE NA OTHER 0.00
4 2018-03-31 PINK NA OTHER 0.00
Questions: 1, why can't i get the variable/DF 'A' to fill down each column based on the information i supplied and 2 how do i manually fill in the date (via a variable) down the column 'PERIOD DT'? 问题:1,为什么我无法根据提供的信息获取变量/ DF'A'来填充每一列,以及2我如何手动(通过变量)在“ PERIOD DT”列下填写日期?
Your missing
list has 5 elements while other lists have only 1. All the lists in the dict that you pass for dataframes should have same number of elements. 您的
missing
列表包含5个元素,而其他列表仅包含1个元素。 Consider below approach. 考虑以下方法。
A = pd.DataFrame({'PERIOD DT': list(pd.to_datetime(currdate)) * len(missing),
'TEAM': missing,
'REGION': ['NA']* len(missing),
'SECTOR': ['OTHER'] * len(missing),
'REVENUE': [0] * len(missing)
})
Output: 输出:
PERIOD DT TEAM REGION SECTOR REVENUE
0 3/31/2018 GREEN NA OTHER 0
1 3/31/2018 YELLOW NA OTHER 0
2 3/31/2018 RED NA OTHER 0
3 3/31/2018 BLUE NA OTHER 0
4 3/31/2018 PINK NA OTHER 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.