Python 按重复顺序填充列
我有一个如下所示的数据框,我想在重复模式(4组)中插入某些值。因此,我的数据帧看起来像“df2”(显示在“df”下面)。我希望此模式继续,直到行结束(在本例中是向下10行) 我们可以在这里使用:Python 按重复顺序填充列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个如下所示的数据框,我想在重复模式(4组)中插入某些值。因此,我的数据帧看起来像“df2”(显示在“df”下面)。我希望此模式继续,直到行结束(在本例中是向下10行) 我们可以在这里使用: 另一种方法是基于NumPy的,它是对df.shape[0]之前的序列进行排序。请注意,如文档中所述,此方法使用输入数组的重复副本填充新数组: a = np.array(['MS', 'MU', 'MN','MO']) df['Code'] = np.resize(a, df.shape[0])
另一种方法是基于
NumPy
的,它是对df.shape[0]之前的序列进行排序。请注意,如文档中所述,此方法使用输入数组的重复副本填充新数组:
a = np.array(['MS', 'MU', 'MN','MO'])
df['Code'] = np.resize(a, df.shape[0])
第一种方法的工作原理是创建一个将索引映射到序列的字典,其中迭代器已经循环了尽可能多的次数,以适应数据帧的
索引
大小。这是因为zip
将合并两个iterables中的元素,直到第一个iterables用尽为止,第一个iterables总是非无限的,很明显:)
然后,我们可以通过将索引与创建的字典映射来将其分配给新列这不是最优雅的解决方案,但非常直观
#create a dataframe with 10 rows
import pandas as pd
df = pd.DataFrame()
df['ID'] = [1,2,3,4,5,6,7,8,9,10]
#create a list containg the seqence of values we want to repeat
values = ['a','b','c','d']
#define the number of times this series can be repeated for a given dataframe length
n_repeats = len(df) % len(values)
#define new column as a list
repeated_values = []
#repeatedly extend the list
for iteration in range(1,n_repeats+2):
repeated_values.extend(values)
#create a column which takes the repeated values and stops at the length of the dataframe
df['repeated_values'] = repeated_values[0:len(df)]
print(df)
ID Descrip Code
0 1 NaN MS
1 2 3.0 MU
2 3 4.0 MN
3 4 7.0 MO
4 5 NaN MS
5 6 11.0 MU
6 7 NaN MN
7 8 20.0 MO
8 9 22.0 MS
9 10 15.0 MU
a = np.array(['MS', 'MU', 'MN','MO'])
df['Code'] = np.resize(a, df.shape[0])
print(dict(zip(df.index, cycle(i))))
# {0: 'MS', 1: 'MU', 2: 'MN', 3: 'MO', 4: 'MS', 5: 'MU'...
#create a dataframe with 10 rows
import pandas as pd
df = pd.DataFrame()
df['ID'] = [1,2,3,4,5,6,7,8,9,10]
#create a list containg the seqence of values we want to repeat
values = ['a','b','c','d']
#define the number of times this series can be repeated for a given dataframe length
n_repeats = len(df) % len(values)
#define new column as a list
repeated_values = []
#repeatedly extend the list
for iteration in range(1,n_repeats+2):
repeated_values.extend(values)
#create a column which takes the repeated values and stops at the length of the dataframe
df['repeated_values'] = repeated_values[0:len(df)]