pandas insert a new row after every nth row

  • Last Update :
  • Techknowledgy :

You can seelct rows by slicing, add 1 to column C_Type and 0.5 to index, for 100% sorrect slicing, because default method of sorting in DataFrame.sort_index is quicksort. Last join together, sort index and create default by concat with DataFrame.reset_index and drop=True:

df['C_Type'] = df['C_Type'].astype(int)

df2 = (df.iloc[3::4]
   .assign(C_Type = lambda x: x['C_Type'] + 1, E_Code = np.nan)
   .rename(lambda x: x + .5))
df1 = pd.concat([df, df2], sort = False).sort_index().reset_index(drop = True)
print(df1)
L_Type L_ID C_Type E_Code
0 0 1 1 9.0
1 0 1 2 9.0
2 0 1 3 9.0
3 0 1 4 9.0
4 0 1 5 NaN
5 0 2 1 2.0
6 0 2 2 2.0
7 0 2 3 2.0
8 0 2 4 2.0
9 0 2 5 NaN
10 0 3 1 3.0
11 0 3 2 3.0
12 0 3 3 3.0
13 0 3 4 3.0
14 0 3 5 NaN

Suggestion : 2

I'd use iloc, which takes a row/column slice, both based on integer position and following normal python syntax. If you want every 5th row:,How to print a list with integers without the brackets, commas and no quotes?,C pointers and arrays: [Warning] assignment makes pointer from integer without a cast,Dataframe.resample() works only with timeseries data. I cannot find a way of getting every nth row from non-timeseries data. What is the best method?

I'd use iloc, which takes a row/column slice, both based on integer position and following normal python syntax. If you want every 5th row:

df.iloc[::5,: ]

Suggestion : 3

I'd use iloc, which takes a row/column slice, both based on integer position and following normal python syntax. If you want every 5th row:,A solution I came up with when using the index was not viable ( possibly the multi-Gig .csv was too large, or I missed some technique that would allow me to reindex without crashing ). Walk through one row at a time and add the nth row to a new dataframe.,Pandas Dataframes merging thru iterations. How to avoid lists and rows of headers,Is there an easier way to do a calculation using every nth row of pandas dataframe?

I'd use iloc, which takes a row/column slice, both based on integer position and following normal python syntax. If you want every 5th row:

df.iloc[::5,: ]

A solution I came up with when using the index was not viable ( possibly the multi-Gig .csv was too large, or I missed some technique that would allow me to reindex without crashing ).
Walk through one row at a time and add the nth row to a new dataframe.

import pandas as pd
from csv
import DictReader

def make_downsampled_df(filename, interval):
   with open(filename, 'r') as read_obj:
   csv_dict_reader = DictReader(read_obj)
column_names = csv_dict_reader.fieldnames
df = pd.DataFrame(columns = column_names)

for index, row in enumerate(csv_dict_reader):
   if index % interval == 0:
   print(str(row))
df = df.append(row, ignore_index = True)

return df
df.drop(labels = df[df.index % 3 != 0].index, axis = 0) # every 3 rd row(mod 3)

I had a similar requirement, but I wanted the n'th item in a particular group. This is how I solved it.

groups = data.groupby(['group_key'])
selection = groups['index_col'].apply(lambda x: x % 3 == 0)
subset = data[selection]

Adding reset_index() to metastableB's answer allows you to only need to assume that the rows are ordered and consecutive.

df1 = df[df.reset_index().index % 3 != 0] # Excludes every 3 rd row starting from 0
df2 = df[df.reset_index().index % 3 == 0] # Selects every 3 rd row starting from 0

There is an even simpler solution to the accepted answer that involves directly invoking df.__getitem__.

df = pd.DataFrame('x', index = range(5), columns = list('abc'))
df

a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x

For example, to get every 2 rows, you can do

df[::2]

a b c
0 x x x
2 x x x
4 x x x

There's also GroupBy.first/GroupBy.head, you group on the index:

df.index // 2
# Int64Index([0, 0, 1, 1, 2], dtype = 'int64')

df.groupby(df.index // 2).first()
      # Alternatively,
      # df.groupby(df.index // 2).head(1)

         a b c 0 x x x 1 x x x 2 x x x

Suggestion : 4

3 days ago Nov 08, 2021  · We can use the following syntax to insert a row of values into the first row of a pandas DataFrame: #insert values into first row of DataFrame df2 = … , 4 days ago Python Programming Overview. Summary: You have learned in this article how to concatenate and stack a new row to a pandas DataFrame to create a union between a DataFrame and a … , Add a Python list object as a row to the existing pandas DataFrame object using DataFrame.loc [] method. l_row = [118,"Kanika",7.88,"EE","Varanasi"] df.loc = l_row print('Modified Sample pandas DataFrame:n') , 3 days ago Jul 03, 2022  · You can use the following basic syntax to apply a function to every row in a pandas DataFrame: df ['new_col'] = df.apply(lambda x: some function, axis=1) This syntax …


  c1 c2 0 a b 1 c d 2 e f 3 g h

In[2]: df Out[2]: c1 c2 0 a b 1 c d 2 e f 3 g h
  c1 c2 0 a b 1 c d 2 e f 3 g h
  c1 0 a 1 b 2 c 3 d 4 e...
second_col_items = [df[['1']].iloc[i].item() for i in range(0, len(df.index))]
In[2]: df Out[2]: c1 c2 0 a b 1 c d 2 e f 3 g h

Suggestion : 5

We can insert a new row as the last row to a Pandas Dataframe using pandas.DataFrame.loc as shown in the following code:-,You can also add a new row to a Pandas Dataframe using pandas.concat() by converting the row into a Dataframe and then concatenating it to the existing Dataframe as under:-,In this post, we will learn to insert/add a new row to an existing Pandas Dataframe using pandas.DataFrame.loc, pandas.concat() and numpy.insert(). Using these methods you can add multiple rows/lists to an existing or an empty Pandas DataFrame.,Now assume that we need to append the following list as a new row to the Pandas Dataframe.

First of all, we will create a Pandas Dataframe from two Pandas Series objects as under :-

s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
cols = ["A", "B", "C"]

df = pd.DataFrame([list(s1), list(s2)], columns = cols)

A B C
0 1 2 3
1 4 5 6

   [2 rows x 3 columns]

Now assume that we need to append the following list as a new row to the Pandas Dataframe.

new_row = [7, 8, 9]

We can insert a new row as the last row to a Pandas Dataframe using pandas.DataFrame.loc as shown in the following code:-

df.loc[-1] = new_row

A B C
0 1 2 3
1 4 5 6
   -
   1 7 8 9

[3 rows x 3 columns]

You can also add a new row to a Pandas Dataframe using pandas.concat() by converting the row into a Dataframe and then concatenating it to the existing Dataframe as under:-

new_df = pd.DataFrame([new_row], columns = cols)

A B C
0 7 8 9

   [1 row x 3 columns]

df = pd.concat([df, new_df])

A B C
0 1 2 3
1 4 5 6
0 7 8 9

   [3 rows x 3 columns]

Here, the new Pandas Dataframe has preserved the index of the row, but you can reset the index while concatenating by using the argument ‘ignore_index = True’ in pandas.concat().

df = pd.concat([df, new_df], ignore_index = True)

A B C
0 1 2 3
1 4 5 6
2 7 8 9

   [3 rows x 3 columns]

Suggestion : 6

Left click on one of the selected cells.,Follow us on social media to stay up to date with the latest in Microsoft Excel!,All the zero’s in our helper column should now be selected and we can now insert our rows.,In the Editing section, press the Find & Select button.

E2
= MOD(ROW(E2) - ROW($E$1) - 1, N)