How can I add and drop columns and rows in a pandas dataframe followed by saving it down into a csv (adding and dropping works but doesn't save)?
Question:
So I have some code that drops a few columns in a pandas dataframe and add one column to the pandas dataframe:
import pandas as pd
df = pd.read_csv('TradedInstrument_20230331_real.txt', sep='t', encoding='unicode_escape')
cols = ['stopTrdgAllag', 'stopTrdgsAllowTrdg', 'stopTrdleInTrdg', 'presholdChf', 'billentCode', 'preTradeBsholdChf', 'billinntDesc']
x = ['e'] * len(df.index)
df['new_col'] = x
df.drop(columns=cols).to_csv('output.csv', index=False)
So what I am trying to do is add a new column new_col
with e
as entries in each row. And also to drop all of the columns that are in cols
. While the code works fine to achieve this, when I run the above, in the output, only the columns are dropped and the row is NOT added!
I can kind of see why this is, it is because the to_csv
only incorporates the dropping of the columns. So how can I add the adding of the new column too?
Answers:
You have to specify axis.
can you try
df.drop(columns=cols,axis = 1).to_csv('output.csv', index=False)
So I have some code that drops a few columns in a pandas dataframe and add one column to the pandas dataframe:
import pandas as pd
df = pd.read_csv('TradedInstrument_20230331_real.txt', sep='t', encoding='unicode_escape')
cols = ['stopTrdgAllag', 'stopTrdgsAllowTrdg', 'stopTrdleInTrdg', 'presholdChf', 'billentCode', 'preTradeBsholdChf', 'billinntDesc']
x = ['e'] * len(df.index)
df['new_col'] = x
df.drop(columns=cols).to_csv('output.csv', index=False)
So what I am trying to do is add a new column new_col
with e
as entries in each row. And also to drop all of the columns that are in cols
. While the code works fine to achieve this, when I run the above, in the output, only the columns are dropped and the row is NOT added!
I can kind of see why this is, it is because the to_csv
only incorporates the dropping of the columns. So how can I add the adding of the new column too?
You have to specify axis.
can you try
df.drop(columns=cols,axis = 1).to_csv('output.csv', index=False)