read.csv, header on first line, skip second line
This should do the trick:
all_content = readLines("file.csv")
skip_second = all_content[-2]
dat = read.csv(textConnection(skip_second), header = TRUE, stringsAsFactors = FALSE)
The first step using readLines
reads the entire file into a list, where each item in the list represents a line in the file. Next, you discard the second line using the fact that negative indexing in R means select all but this index
. Finally, we feed this data to read.csv
to process it into a data.frame
.
How to skip second line is csv file while maintaining first line as column names with read_csv?
You can just read in twice - once to get the names, and then to get the data.
library(readr)
library(dplyr)
csv_file <- "mpg,cyl,disp,hp,drat,wt
mpg,cyl,disp,hp,drat,wt
21.0,6,160,110,3.90,2.875
22.8,4,108,93,3.85,2.320
21.4,6,258,110,3.08,3.215
18.7,8,360,175,3.15,3.440
18.1,6,225,105,2.76,3.460"
df_names <- read_csv(csv_file, n_max = 0) %>% names()
df_names
#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt"
df <- read_csv(csv_file, col_names = df_names, skip = 2)
df
#> # A tibble: 5 x 6
#> mpg cyl disp hp drat wt
#> <dbl> <int> <int> <int> <dbl> <dbl>
#> 1 21.0 6 160 110 3.90 2.875
#> 2 22.8 4 108 93 3.85 2.320
#> 3 21.4 6 258 110 3.08 3.215
#> 4 18.7 8 360 175 3.15 3.440
#> 5 18.1 6 225 105 2.76 3.460
Not able to read csv while skipping first row and using second as header in pandas for raw tick data of symbols
in skiprows you need to give number of rows you want to skip from the top of your csv
use utf-16
df = pd.read_csv(cwd + folder + name +'.csv',delimiter=';', encoding='utf-16', skiprows=1)
for more info:
To check the encoding i have checked in libreoffice. if you open with
libreoffice in its starting window you can choose delimiter, in which it
also showsutf
encoding of that file.
How to skip the first line of a CSV file and make the second line the header
I don't think there's an elegant way of doing it, but it can be done:
require "csv"
# Create a stream using the original file.
# Don't use `textmode` since it generates a problem when using this approach.
file = File.open "file.csv"
# Consume the first CSV row.
# `\r` is my row separator character. Verify your file to see if it's the same one.
loop { break if file.readchar == "\r" }
# Create your CSV object using the remainder of the stream.
csv = CSV.new file, headers: true
Import csv via fread skipping first line and header on second line
fread
is intended to be similar to read.table
, read.csv
, etc. so
data <- fread("C:/1.csv", skip=1, header=T)
will work.
Skip specific rows using read.csv in R
One way to do this is using two read.csv
commands, the first one reads the headers and the second one the data:
headers = read.csv(file, skip = 1, header = F, nrows = 1, as.is = T)
df = read.csv(file, skip = 3, header = F)
colnames(df)= headers
I've created the following text file to test this:
do not read
a,b,c
previous line are headers
1,2,3
4,5,6
The result is:
> df
a b c
1 1 2 3
2 4 5 6
skip second row of dataframe while reading csv file in python
Your code will ommit the first two lines of your csv.
If you want the second line to be ommitted (but the first one included) just do this minor change:
imdb_data = pd.read_csv('IMDB_data.csv', encoding = "ISO-8859-1",skiprows = [1])
Related Topics
How to Use R with Google Colaboratory
Extract Matrix Column Values by Matrix Column Name
Format Number as Fixed Width, with Leading Zeros
Create Categories by Comparing a Numeric Column with a Fixed Value
Mean of Each Element of a List of Matrices
How to Connect R with Access Database in 64-Bit Window
Why Is Apply() Method Slower Than a for Loop in R
How to Create Grouped Barplot with R
Why Do Some Unicode Characters Display in Matrices, But Not Data Frames in R
Determine the Data Types of a Data Frame's Columns
In 'Knitr' How to Test for If the Output Will Be PDF or Word
Rstudio Rmarkdown: Both Portrait and Landscape Layout in a Single PDF
Perform a Semi-Join with Data.Table
Operator == Inconsistent in Logical Columns in Data.Table
Ggplot2 Heatmap with Colors for Ranged Values
How to Draw Stacked Bars in Ggplot2 That Show Percentages Based on Group
Split a Column of Concatenated Comma-Delimited Data and Recode Output as Factors