Nov-25-2022, 09:53 PM
is there a way, with pandas or otherwise - to specify a range of columns to be deleted without writing the name/header of the columns ? as in pop() and drop()
import pandas as pd data = { 1: [1, 19, 20, 21, 25, 29, 30, 31, 30, 29, 31], 2: [2, 10, 20, 20, 20, 10, 10, 20, 20, 10, 10], 3: [3, 10, 20, 20, 20, 10, 10, 20, 20, 10, 10], 4: [4, 10, 20, 20, 20, 10, 10, 20, 20, 10, 10], } df = pd.DataFrame(data)Delete columns from 2 to 4,adjust as needed.
>>> df 1 2 3 4 0 1 2 3 4 1 19 10 10 10 2 20 20 20 20 3 21 20 20 20 4 25 20 20 20 5 29 10 10 10 6 30 10 10 10 7 31 20 20 20 8 30 20 20 20 9 29 10 10 10 10 31 10 10 10 >>> df.drop(df.columns[2:4], axis=1, inplace=True) >>> df 1 2 0 1 2 1 19 10 2 20 20 3 21 20 4 25 20 5 29 10 6 30 10 7 31 20 8 30 20 9 29 10 10 31 10
import pandas as pd data = pd.read_csv('/home/tal/investing/allstocks.csv') print("Original 'allstocks.csv' CSV Data: \n") print(data) df = pd.DataFrame(data) df.drop(df.columns[3:], axis=1, inplace=True) df.to_csv('/home/tal/investing/allstocks.csv') print("\nCSV Data after deleting the column 'year':\n") print(df)and attached is a screenshot of the result,
index=False
df.to_csv('/home/tal/investing/allstocks.csv', index=False)
Error:Name 'columnSeriesObj' can be undefined
here's the code:import pandas as pd data = pd.read_csv('/home/tal/investing/allstocks.csv') print("Original 'allstocks.csv' CSV Data: \n") print(data) df = pd.DataFrame(data) for column in df['Ticker']: columnSeriesObj = df['Ticker'] print('Column Contents : ', columnSeriesObj.values) columnSeriesObj.to_csv('/home/tal/investing/Ticker.csv', index=False)although i got the wanted result, it produced the file i want with only one of the columns (the Ticker column), but why in the IDE it gives me this error ?
(Nov-26-2022, 05:23 PM)astral_travel Wrote: [ -> ]although i got the wanted result, it produced the file i want with only one of the columns (the Ticker column), but why in the IDE it gives me this error ?The loop make litte sense,this dos the same
df['Ticker'].to_csv('/home/tal/investing/Ticker.csv', index=False)The varibale
columnSeriesObj
is created 3 times in the,where only the last one is used.columnSeriesObj.values
just print the same values 3 times.vectorized solution
(that's build-in and there are many)diffrent way to program
than the standar Python way,you are new to both so it's can be confusing.for loop
used.