1

I have a csv file. I am using it to do some machine learning stuffs on it. But, my dataset in csv file is very large. For doing some practice stuffs, I want to reduce its size. Like, in my original csv file, I have 200000 data items. But for practice I want to create a csv file with first 100 data items. How can I do this with python.

Note: I want a new csv file of 100 data items.

I tried using to_csv method of pandas library.

x.to_csv('tempo.csv', sep='\t', encoding='utf-8')

The above method made a new file. But, it gives me error for following code:

x['Keywords']=x['Keywords'].map(lambda x: convert(x))

Error is:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  x['Keywords']=x['Keywords'].map(lambda x: convert(x))
  • Or this one probably has a better answer: https://stackoverflow.com/questions/69203560/how-do-i-read-the-first-10-rows-of-a-pandas-dataframe – Code-Apprentice Oct 30 '22 at 06:21
  • This just stores n data items in a variable. I want to create a new file which will contain this n data items. – Om_Telavane Oct 30 '22 at 06:39
  • You can write the resulting dataframe to a file with `to_csv()` just as you already have in your code here. – Code-Apprentice Oct 30 '22 at 06:51
  • But, by doing this, I get the same error on each line. Error is given in question. – Om_Telavane Oct 30 '22 at 06:58
  • I think the error you posted in your question has nothing to do with reducing the size of your CSV file, as you state in your title. You should post a new question just for the error. Be sure to include a [mcve] that reproduces the exact error message you ask about. – Code-Apprentice Oct 30 '22 at 07:03

0 Answers0