Location | Category | Count |
---|---|---|
Duliajan Area | PD | 1 |
Duliajan Area | SE | 3 |
HAPJAN | COL | 1 |
HAPJAN | OTH | 1 |
KATHALGURI | COL | 1 |
KATHALGURI | DP-PD | 1 |
How can I get count in a variable. (SE - 3),(PD - 1) like that
CodePudding user response:
Maybe you can use to_dict
:
>>> tuple(df.set_index('Category')['Count'].to_dict().items())
(('PD ', 1), ('SE ', 3), ('COL ', 1), ('OTH ', 1), ('DP-PD ', 1))
CodePudding user response:
# import pandas library
import pandas as pd
# dictionary
dict = {'Location': ['Duliajan Area', 'Duliajan Area',
'HAPJAN', 'HAPJAN'],
'Category': ['PD', 'SE',
'COL', 'OTH']
'Count': [1, 3,
1, 1]}
# create a dataframe object
df = pd.DataFrame(dict)
# show the dataframe
print(df)
# list of values of 'category' column
category_list = df['Category'].tolist()
# show the list
print(category_list)
CodePudding user response:
For this you can use pandas library and can use for loop that will loop for the length of column and you can find the lenght by using .shape
method
To get (SE - 3),(PD - 1)
format you can use f"({category_column[i]}-{count_column[i]})"
So your final code should be: import pandas as pd
df = pd.read_csv("file3.csv")
category_column = df[" Category"]
count_column = df[" Count"]
num_rows = df.shape[0]
new_list = []
for i in range(num_rows):
new_list.append(f"({category_column[i]}-{count_column[i]})")