So I need to output a txt file that in one row contains the city-value of all companies in said industry rounded on 2 decimal places. Code works as intended I just need to round them to 2 decimal places. I have some guesses in what seems to be a problem with rounding them but I just can't figure it out. PS I'm new to python and coding in general so I'm sorry if my question seems dumb.
import os
try:
import pandas as pd
except ImportError:
os.system("pip install pandas")
def main(filename='statups.csv'):
# reading the csv
df = pd.read_csv(filename)
# grouping by industry name and city and summing the value
ast = df.groupby(['Industry', 'City'], as_index=False)['Valuation
($B)'].sum()
# sorting the columns
ast = ast.sort_values(by=['Industry', 'Valuation ($B)'], ascending=
[True, False])
# could have done with pandas but ran into saving group by to csv function error
# and went with inefficient way
# creating the dictionary to hold each industry value
my_dict = {}
for val in ast['Industry'].unique():
val = val.replace(" ", '_')
my_dict[val] = []
# storing the value in dictionary
for index, row in ast.iterrows():
val = row['Industry'].replace(" ", '_')
my_dict[val].append(str(row['City']) + "-" + str(row['Valuation
($B)']))
# saving the value from the dictionary
for k in my_dict.keys():
with open(f'{k} + ".txt", 'w') as textfile:
for element in my_dict[k]:
textfile.write('{:0.3f}'.format(element) + "\n")
if __name__ == '__main__':
filename = input('Enter filename: ')
main(filename)