[英]Adding new column based on index list in pandas
I have a pandas dataframe like,我有一个 pandas dataframe 之类的,
pd.DataFrame({'f1':[23,56,7, 56,34, 98],
'f2':[32,85,27, 36,64, 60]})
f1 f2
0 23 32
1 56 85
2 7 27
3 56 36
4 34 64
5 98 60
and based on an index list like index_list = [2, 4]
I want to add a new column to original datafram like following,并基于index_list = [2, 4]
类的索引列表,我想向原始数据框添加一个新列,如下所示,
new_column f1 f2
0 0 23 32
1 0 56 85
2 0 7 27
3 1 56 36
4 1 34 64
5 2 98 60
Note: index list shows the locations that new_column should increase 1 integer up.注意:索引列表显示 new_column 应该增加 1 integer 的位置。
A simple way is to use cumsum
:一个简单的方法是使用cumsum
:
df = pd.DataFrame(index=range(6))
index_list = [2, 4]
index_list = [x+1 for x in index_list]
df["new"] = 0
df["new"].loc[index_list] = 1
df["new"].cumsum()
which gives:这使:
0 0
1 0
2 0
3 1
4 1
5 2
df.loc[[x+1 for x in index_list], 'new_column'] = 1
df.new_column = df.new_column.fillna(0).cumsum()
print(df)
Output: Output:
f1 f2 new_column
0 23 32 0.0
1 56 85 0.0
2 7 27 0.0
3 56 36 1.0
4 34 64 1.0
5 98 60 2.0
Here's a way to get the exact output specified in your question:这是获得问题中指定的确切 output 的方法:
df2 = df.reindex(pd.Series(index_list)+1)
df = df.assign(new_column=pd.Series(range(1, 1+len(df2)), index=df2.index))[['new_column'] + list(df.columns)]
df.new_column = df.new_column.ffill().fillna(0).astype(int)
Output: Output:
new_column f1 f2
0 0 23 32
1 0 56 85
2 0 7 27
3 1 56 36
4 1 34 64
5 2 98 60
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.