简体   繁体   English

使用pandas从python中的现有csv文件生成新csv文件的指定数字数据帧的方法

[英]way to generate a specified number dataframe of new csv file from existing csv file in python using pandas

I have large data-frame in a Csv file sample1 from that i have to generate a new Csv file contain only 100 data-frame.i have generate code for it.but i am getting key Error the label[100] is not in the index? 我在Csv文件sample1中有大数据帧,我必须生成一个新的Csv文件只包含100个data-frame.i已为它生成代码。但是我得到键错误标签[100]不在指数? I have just tried as below,Any help would be appreciated 我刚尝试如下,任何帮助将不胜感激

import pandas as pd
data_frame = pd.read_csv("C:/users/raju/sample1.csv")
data_frame1 = data_frame[:100]
data_frame.to_csv("C:/users/raju/sample.csv")`

` `

The correct syntax is with iloc : 正确的语法是iloc

data_frame.iloc[:100]

A more efficient way to do it is to use nrows argument who purpose is exactly to extract portions of files. 一种更有效的方法是使用nrows参数,其目的正是提取文件的一部分。 This way you avoid wasting resources and time parsing useless rows: 这样可以避免浪费资源和时间来解析无用的行:

import pandas as pd
data_frame = pd.read_csv("C:/users/raju/sample1.csv", nrows=101)  # 100+1 for header
data_frame.to_csv("C:/users/raju/sample.csv")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM