简体   繁体   English

如何将列的一部分添加到新的 Pandas 数据框中?

[英]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.

相关问题 如何向具有不同列号的 Pandas 数据框添加新行? - How to add new rows to a Pandas Data Frame with varying column numbers? 在熊猫数据框中添加新列 - Add new column in pandas data frame 在带有类别的 Pandas 数据框中添加新列 - add new column in Pandas Data frame with categories 如何通过从另一列中的句子中提取单词来在 pandas 数据框中创建一个新列? - How can I create a new column in a pandas data frame by extracting words from sentences in another column? 如何根据列的相等部分将切片分配给pandas数据框? - How do I assign tiles to a pandas data frame based on equal parts of a column? 如何在 Pandas 数据框中添加条目数作为新行? - How can I add number of entries as a new row in pandas data frame? 如何使用 pandas 数据框将数据框的每一列值添加到一张一张的新工作表中 - How to add each column of a data frame values in one by one new sheets using pandas data frame 如何在 pandas 数据框中创建新列 - How to create a new column in a pandas data frame 如何比较两个不同数据框的两列并添加新的结果列 - How can I compare two columns of two different data frame and add new resultant column 更改 pandas 数据框以从 pandas 中的数据框添加最大列 -> 一年中每个月的最大值。 我怎样才能做到这一点? - Change pandas data frame to add max column -> maximum value for each month of the year from a data frame in pandas. How can I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM