1

Not sure if I am doing this right - first post here so please be gentle :)

Se below picture.

Print screen from my Jupyter Notebook

What I am trying to do is to create a new dataframe from the df_Grundinladdning['Datan'] dataframe which only include the rows that contain the string "#TRANS".

Magnus Eke
  • 33
  • 4

3 Answers3

1

Here's a way to do that:

df = pd.DataFrame({"Datan": ["x", "TRANS y", "z", "TRANS u", "v", "TRANS w"]})
print(df)

new_df = df[df.Datan.str.contains("TRANS")]
print(new_df)

Results:

(original dataframe)
     Datan
0        x
1  TRANS y
2        z
3  TRANS u
4        v
5  TRANS w

(new dataframe)
     Datan
1  TRANS y
3  TRANS u
5  TRANS w
Roy2012
  • 11,755
  • 2
  • 22
  • 35
0

The right method is described here. The loop, even if it did not have syntax errors, would be very very slow.

Igor Rivin
  • 4,632
  • 2
  • 23
  • 35
0

You don't need to loop over the dataframe you can get the result dataframe easily with this:

df_transOnly= df_Grundinladdning[df_Grundinladdning["Datan"].str.contains('#TRANS')]
df_transOnly #for printing df

So you will get the needed dataframe like this:

      Datan
5     #TRANS232
12    #TRANS455
20    #TRANS3144
104   #TRANS1234
500   #TRANS213
SE7A.
  • 96
  • 7