简体   繁体   English

使用 itterrows 向 Pandas df 添加行

[英]Add rows to pandas df using itterrows

I have the following pandas datframe我有以下熊猫数据框

1

For each country I wish to create as many rows as the number of years it exists.对于每个国家,我希望创建与它存在的年数一样多的行。 For instance, the US will have 201 rows, Canada 95 and so forth.例如,美国将有 201 行,加拿大将有 95 行等等。

I thought of doing something like:我想过做这样的事情:

for row in df.iterrows():
    for range(row['styear'], row['endyear']):
        df.append(row)

Any ideas how to make this work?任何想法如何使这项工作?

You can create a new column with the range of years, and then explode that column您可以创建一个具有年份range的新列,然后explode该列

# sample dataframe
df = pd.DataFrame({
    'country': ['United States', 'Canada', 'Bahamas', 'Cuba'],
    'styear': [1816, 1920, 1973, 1902],
    'endyear': [2016, 2016, 2016, 1906]
})


df['allyears'] = [range(start, end+1)
    for start, end in zip(df.styear, df.endyear)]
df = df.explode('allyears')
print(df)

Output输出

          country  styear  endyear allyears
0   United States    1816     2016     1816
0   United States    1816     2016     1817
0   United States    1816     2016     1818
0   United States    1816     2016     1819
0   United States    1816     2016     1820
..            ...     ...      ...      ...
3            Cuba    1902     1906     1902
3            Cuba    1902     1906     1903
3            Cuba    1902     1906     1904
3            Cuba    1902     1906     1905
3            Cuba    1902     1906     1906

[347 rows x 4 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM