Reputation:
I have a structure like this; used to create a pandas dataframe:
my_dict = { 'name' : ["joe", "jack", "jill", "joan", "jesse","jacob", "jonas"],
'age' : [20,27, 35, 55, 18, 21, 35],
'designation': ["VP", "CEO", "CFO", "VP", "VP", "CEO", "MD"]}
I create a dataframe via df = pd.DataFrame(my_dict)
; so far so good.
Now, I would like to create variants of this CSV, let's say to change names or age; or maybe have some variants that include also salary for example.
I did solve the problem by creating brand new dictionaries and then export them directly in CSV, but I would like to use pandas dataframes, so I can read from CSV file, make changes in the dataframe and then export as variant of the loaded csv. Am I wasting time doing this with pandas, since I can just create and export a dictionary as CSV?
EDIT: As example, I would like to change the age and names as in the original dictionary. So I would like to extract every value on those 2 columns and replace them with new values (so for example I have already 2 lists with the new names, both lists have the same number of elements as the original dictionary)
newname = ["Mike", "Frank", "Andrew", "Marge", "Alphonse","Roy", "Albert"]
newage = [22,32,34,43,21,55,66]
Another variant may be while adding or replacing a column and its data, so for example if I want to replace the designation
column with salary
; I would pass a new dictionary with the new field for the column and the related data like this:
newfield = {"Salary": [22, 32, 21, 14, 55, 34, 66]}
Upvotes: 0
Views: 6408
Reputation: 712
If you just want to make some modifications/variants to/off the initial data set you can make it with pandas as save it as a different csv file (or append to the original one). But as others have mentioned we might need to know in more detail what you want to accomplish.
1 turn your dictionary into a dataframe ans saves it as a csv file:
2 make changes to the dataframe and save as a different csv
3 change columns in one of you variance to include the new salary and drop the designation.
you could also automate this much more and create as many variants as you need every-time you ran your file. We just have to know in more detail what you want to accomplish.
Upvotes: 2