Create a for statement code to modify the contents of the pandas data frame

Asked 2 years ago, Updated 2 years ago, 452 views

Hello, I'm working on the data frame using Pandas, but it hasn't been solved, so I'm posting a question like this.

Data frames need to be loaded and modified for each row. For example, if it's row 1, column 1 contents / column 1 contents / column 2 contents / column 1 contents / column 1, column 2, column 2 contents / column 3 contents should be changed. All the values that need to be changed can be changed to nan, but the data frame is not familiar and cannot be solved due to lack of knowledge...

I ask for your help me Thank you.

pandas dataframe python

2022-10-29 00:01

2 Answers

def solution(df):
    for i in range(10):
        for j in range(10):
            if i >= j:
                df.iloc[i,j] = np.nan
    return df

I think you can refer to it and modify it and use it.


2022-10-29 00:01

For example, if you have a table like this (let's say it's a partial representation)

df = pd.DataFrame([[0.000, 0.0224, 0.0123, 0.0446], [0.0224, 0.000, 0.0145, 0.0105], [0.0123, 0.0145, 0.0000, 0.0255], [0.0446, 0.0105, 0.0255 ,0.000]] , columns = [a, b, c, d])

By writing the code df = pd.DataFrame([[nan , 0.0224, 0.0123, 0.0446], [nan, nan, 0.0145, 0.0105], [nan, nan, nan, 0.0255], [nan, nan, nan, nan]] , columns = [a, b, c, d])

I want to create a new ticket that looks like this.


2022-10-29 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.