亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python如何獲取.csv文件中的某一列或者某些列

 更新時(shí)間:2024年02月04日 09:28:18   作者:MqtGhj  
這篇文章主要介紹了python如何獲取.csv文件中的某一列或者某些列問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

獲取.csv文件某一列或者某些列

1.把三個(gè)csv文件中

的feature值整合到一個(gè)文件中,同時(shí)添加相應(yīng)的label。

# -*-coding:utf-8 -*-
import csv;
label1 = '1'
label2 = '2'
label3 = '3'
a = "feature1,feature2,feature3,feature4,feature5,feature6,feature7,feature8,feature9,feature10,label" + "\n"
with open("./dataset/dataTime2.csv", 'a') as rfile:
     rfile.writelines(a)
with open("./dataset/f02.csv", 'rb') as file:
    a = file.readline().strip()
    while a:
        a = a + ',' + label1 + "\n"
        #a = label1 + ',' + a + "\n"
        with open("./dataset/dataTime2.csv", 'a') as rfile:
            rfile.writelines(a)
        a = file.readline().strip()
with open("./dataset/g03.csv", 'rb') as file:
    a = file.readline().strip()
    while a:
        a = a + ',' + label2 + "\n"
        #a = label2 + ',' + a + "\n"
        with open("./dataset/dataTime2.csv", 'a') as rfile:
            rfile.writelines(a)
        a = file.readline().strip()
with open("./dataset/normal05.csv", 'rb') as file:
    a = file.readline().strip()
    while a:
        a = a + ',' + label3 + "\n"
        #a = label3 + ',' + a + "\n"
        with open("./dataset/dataTime2.csv", 'a') as rfile:
            rfile.writelines(a)
        a = file.readline().strip()

2.獲取csv文件中某一列

下面可以獲得label為表頭的列中對(duì)應(yīng)的所有數(shù)值。

filename = "./dataset/dataTime2.csv"
list1 = []
with open(filename, 'r') as file:
    reader = csv.DictReader(file)
    column = [row['label'] for row in reader]

3.獲取csv文件中某些列

下面可以獲得除label表頭的對(duì)應(yīng)列之外所有數(shù)值。

import pandas as pd
odata = pd.read_csv(filename)
y = odata['label']
x = odata.drop(['label'], axis=1) #除去label列之外的所有feature值

4.也可以處理成list[np.array]形式的數(shù)據(jù)

filename = "./dataset/dataTime2.csv"
list1 = []
with open(filename, 'r') as file:
    a = file.readline()
    while a:
        c = np.array(a.strip("\n").split(","))
        list1.append(c)

5.也可以處理成tensor格式數(shù)據(jù)集

# -*-coding:utf-8 -*-
import tensorflow as tf
# 讀取的時(shí)候需要跳過(guò)第一行
filename = tf.train.string_input_producer(["./dataset/dataTime.csv"])
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename)
record_defaults = [[1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], [1.], tf.constant([], dtype=tf.int32)]
col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11= tf.decode_csv(
    value, record_defaults=record_defaults)
features = tf.stack([col1, col2, col3, col4, col5, col6, col7, col8, col9, col10])
with tf.Session() as sess:
  # Start populating the filename queue.
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  trainx = []
  trainy = []
  for i in range(81000):
    # Retrieve a single instance:
      example, label = sess.run([features, col11])
      trainx.append(example)
      trainy.append(label)
  coord.request_stop()
  coord.join(threads)
#最后長(zhǎng)度是81000,trainx是10個(gè)特征

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python和Plotly實(shí)現(xiàn)3D圖形繪制

    Python和Plotly實(shí)現(xiàn)3D圖形繪制

    在當(dāng)今的數(shù)據(jù)分析和可視化領(lǐng)域,Python已經(jīng)成為一種不可或缺的工具,Plotly作為一種高級(jí)的繪圖庫(kù),特別擅長(zhǎng)于創(chuàng)建交互式和3D圖形,下面我們就來(lái)看看Python如何利用Plotly實(shí)現(xiàn)3D圖形繪制吧
    2024-11-11
  • python筆記之mean()函數(shù)實(shí)現(xiàn)求取均值的功能代碼

    python筆記之mean()函數(shù)實(shí)現(xiàn)求取均值的功能代碼

    這篇文章主要介紹了python筆記之mean()函數(shù)實(shí)現(xiàn)求取均值的功能代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • JSON Web Tokens的實(shí)現(xiàn)原理

    JSON Web Tokens的實(shí)現(xiàn)原理

    本文主要介紹了JSON Web Tokens的實(shí)現(xiàn)原理。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-04-04
  • 關(guān)于Python中函數(shù)的幾種形參

    關(guān)于Python中函數(shù)的幾種形參

    這篇文章主要介紹了關(guān)于Python中函數(shù)的幾種形參,python中函數(shù)是非常重要的一個(gè)知識(shí)點(diǎn),想要把函數(shù)學(xué)習(xí)好,就必須要把函數(shù)的參數(shù)學(xué)習(xí)好,這樣才能夠進(jìn)行很好的傳遞參數(shù),發(fā)揮出應(yīng)有的作用,需要的朋友可以參考下
    2023-08-08
  • python爬蟲(chóng)泛濫的解決方法詳解

    python爬蟲(chóng)泛濫的解決方法詳解

    在本篇文章里小編給大家整理了關(guān)于python爬蟲(chóng)泛濫的解決方法詳解內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。
    2020-11-11
  • Python實(shí)現(xiàn)二分查找與bisect模塊詳解

    Python實(shí)現(xiàn)二分查找與bisect模塊詳解

    二分查找又叫折半查找,二分查找應(yīng)該屬于減治技術(shù)的成功應(yīng)用。python標(biāo)準(zhǔn)庫(kù)中還有一個(gè)灰常給力的模塊,那就是bisect。這個(gè)庫(kù)接受有序的序列,內(nèi)部實(shí)現(xiàn)就是二分。下面這篇文章就詳細(xì)介紹了Python如何實(shí)現(xiàn)二分查找與bisect模塊,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-01-01
  • Python將字典轉(zhuǎn)換為XML的方法

    Python將字典轉(zhuǎn)換為XML的方法

    這篇文章主要介紹了Python將字典轉(zhuǎn)換為XML的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-08-08
  • OpenCV:imwrite函數(shù)保存圖片問(wèn)題

    OpenCV:imwrite函數(shù)保存圖片問(wèn)題

    這篇文章主要介紹了關(guān)于OpenCV:imwrite函數(shù)保存圖片問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 詳解Python解決抓取內(nèi)容亂碼問(wèn)題(decode和encode解碼)

    詳解Python解決抓取內(nèi)容亂碼問(wèn)題(decode和encode解碼)

    這篇文章主要介紹了Python解決抓取內(nèi)容亂碼問(wèn)題(decode和encode解碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python異常處理try except過(guò)程解析

    python異常處理try except過(guò)程解析

    這篇文章主要介紹了python異常處理try except過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論