[英]How can I add parts of a column to a new pandas data frame?
So I have a pandas data frame of lenght 90 which isn't important Lets say I have :所以我有一个长度为 90 的熊猫数据框,这并不重要让我说我有:
df
A date
1 2012-01-01
4 2012-02-01
5 2012-03-01
7 2012-04-01
8 2012-05-01
9 2012-06-01
2 2012-07-01
1 2012-08-01
3 2012-09-01
2 2012-10-01
5 2012-11-01
9 2012-12-01
0 2013-01-01
6 2013-02-01
and I have created a new data frame我创建了一个新的数据框
df_copy=df.copy()
index = range(0,3)
df1 = pd.DataFrame(index=index, columns=range((len(df_copy.columns))))
df1.columns = df_copy.columns
df1['date'] = pd.date_range('2019-11-01','2020-01-01' , freq='MS')-pd.offsets.MonthBegin(1)
which should create a data frame like this这应该创建一个这样的数据框
A date
na 2019-10-01
na 2019-11-01
na 2019-12-01
So I use the following code to get the values of A in my new data frame所以我使用以下代码来获取新数据框中 A 的值
df1['A'] = df1['A'].iloc[9:12]
And I want the outcome to be this我希望结果是这样
A date
2 2019-10-01
5 2019-11-01
9 2019-12-01
so I want that the last 3 values are assigned the value that has iloc position 9-12 in the new data frame, the indexes are different and so are the dates in both data frames.所以我希望为最后 3 个值分配在新数据框中具有 iloc 位置 9-12 的值,索引不同,两个数据框中的日期也是如此。 Is there a way to do this because
有没有办法做到这一点,因为
df1['A'] = df1['A'].iloc[9:12]
doesn't seem to work似乎不起作用
According to my knowledge you can solve this by genearting several new data frames据我所知,您可以通过生成几个新的数据框来解决这个问题
df_copy=df.copy()
index = range(0,1)
df1 = pd.DataFrame(index=index, columns=range((len(df_copy.columns))))
df1.columns = df_copy.columns
df1['date'] = pd.date_range('2019-11-01','2019-11-01' , freq='MS')-pd.offsets.MonthBegin(1)
df1['A'] = df1['A'].iloc[9]
Then appending to your original data frame and repeating it is a bit overwhemling but it seems like the only solution i could came up with然后附加到您的原始数据框并重复它有点不知所措,但这似乎是我能想到的唯一解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.