0

I have a dataframe called data6, with 6000 rows, and i want to copy to na 2000 rows data frames, called result, only Month columns values when level columns value are 1. How do create a for loop with this rule?

Now:

in: data6 = df1[['level', 'Month']]
    print(data6)

out:      level   Month
0         1.0  101.52
1         2.0  101.52
2         3.0  101.52
3         1.0  111.89
4         2.0  111.89

Expected after the for loop:

in: print(result)

out:      level   Month
0         1.0  101.52
1         1.0  111.89
2         1.0  112.27
3         1.0  89.57
4         1.0  110.35
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • What is the datatype of level column? Float or string? – Scott Boston Jun 17 '20 at 16:50
  • Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – Trenton McKinney Jun 17 '20 at 16:53

1 Answers1

0
# if level is a float
result = data6[data6.level == 1.0].reset_index(drop=True)

# if level is a string
result = data6[data6.level == '1.0'].reset_index(drop=True)

# if you only want the month column
result = pd.DataFrame(data6.Month[data6.level == 1.0]).reset_index(drop=True)  # or '1.0'
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158