I have a .csv file with many rows and columns. For analysis purposes, I want to select a row number from the dataset and pass it as a dataframe in pandas.
Instead of writing the column names and input values inside a dict, how can I make it faster? Right now I have:
df= pd.read_csv('filename.csv')
df2= pd.DataFrame({'var1': 5, 'var2': 10, 'var3': 15})
var1,var2,var3 are df columns. I want to make a seperate dataframe with df data.
You can either select a random row, or a given row number. Thank you for your help.
CodePudding user response:
df2 = df.iloc[rownum:rownum 1, :]
CodePudding user response:
If you want to filter out data as new dataframe from existing one you can use something like this -
based on particular rows required
df2 = df.iloc[4:5,:]
or data using some condition
df3 = df[df['var1'] < 10]