1

I was wondering how to do something like the code below without using a for.

values = [11,20,23,30,32,35,60...]
for new_value in range(10,10000,10):
    if new_value not in values:
        df.at[new_value, 'a2'] = new_value

What I am trying to do is:

  • I have two lists: values and new_values
  • if an element from new_values is not in values update the df where the new_value is both, the index and the updated value.

Thanks!

EDIT:

the df looks like:

df = pd.DataFrame(index=range(10,1000,10), columns=['a1','a2','a3'])
2
  • Can you show your df ? Commented Apr 29, 2021 at 13:44
  • I have updated the question Commented Apr 29, 2021 at 13:49

2 Answers 2

1

Do you want something like this?

mask = (~df.index.isin(values))
df.loc[mask, 'a2'] = df.loc[mask].index
Sign up to request clarification or add additional context in comments.

Comments

1

Try creating a DataFrame from values where the indices and data are the same, concat this new DataFrame with the original, use groupby.head to get only the first row from each id group, then cleanup order with sort_index:

import pandas as pd

df = pd.DataFrame(index=range(10, 100, 10), columns=['a1', 'a2', 'a3'])

values = [11, 20, 23, 30, 32, 35, 60]
# Convert list into a DataFrame with index and value the same
values_df = pd.DataFrame(data=values, index=values, columns=['a2'])
# Create new dataframe from df and values_df
new_df = pd.concat((df, values_df))

# Group By index and only keep first from each group
# (not using first() since it would overwrite the NaNs)
new_df = new_df.groupby(new_df.index).head(1).sort_index()

# For Display
print(new_df)

Source (df):

     a1   a2   a3
10  NaN  NaN  NaN
20  NaN  NaN  NaN
30  NaN  NaN  NaN
40  NaN  NaN  NaN
50  NaN  NaN  NaN
60  NaN  NaN  NaN
70  NaN  NaN  NaN
80  NaN  NaN  NaN
90  NaN  NaN  NaN

Output (new_df):

     a1   a2   a3
10  NaN  NaN  NaN
11  NaN   11  NaN
20  NaN  NaN  NaN
23  NaN   23  NaN
30  NaN  NaN  NaN
32  NaN   32  NaN
35  NaN   35  NaN
40  NaN  NaN  NaN
50  NaN  NaN  NaN
60  NaN  NaN  NaN
70  NaN  NaN  NaN
80  NaN  NaN  NaN
90  NaN  NaN  NaN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.