Is there a way to store the row no of element values that match a specific value in a specific column of DataFrame as variables?

Asked 2 years ago, Updated 2 years ago, 16 views

Hi, everyone.

Is there a way to store the number of element values that match a specific value in a specific column of DataFrame as variables?

You are trying to save Row numbers as variables and then try to store elements corresponding to the row values in different columns as different variables. I'd appreciate your help.

python

2022-09-20 15:19

1 Answers

city = [Seoul, Busan, Daegu, Daejeon, Gwangju, Daejeon]
df = pd.DataFrame (columns = ['city'], index = range (len(city)))))
df['City'] = city
print(df)

print('-' * 10)

city in [Busan, Daejeon]:
    cityIndex = df.index[df['city'] == city.tolist() #LISTTYPE
    print(city, cityIndex)


2022-09-20 15:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.