天天看點

Pandas讀取.csv操作

常用操作:取行,取列,删除,儲存

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    #取指定幾列
    new_data = data[["RVR_1A", "MOR_1A"]]
    #       RVR_1A  MOR_1A
    # 0       3000    3000
    # 1       3000    3000
    # 2       3000    3000
    # 3       3000    3100
    # 4       3000    3100
    # ...      ...     ...
    # 5750     125      50
    # 5751     125      50
    # 5752     125      50
    # 5753     125      50
    # 5754     125      50
    #
    # [5755 rows x 2 columns]
    print(new_data)

    # .loc取某些行
    new_data = new_data.loc[0:]
    #       RVR_1A  MOR_1A
    # 0       3000    3000
    # 1       3000    3000
    # 2       3000    3000
    # 3       3000    3100
    # 4       3000    3100
    # ...      ...     ...
    # 5750     125      50
    # 5751     125      50
    # 5752     125      50
    # 5753     125      50
    # 5754     125      50
    #
    # [5755 rows x 2 columns]
    print(new_data)   

data = read_csv()
           
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    #取指定幾列
    new_data = data[["RVR_1A", "MOR_1A"]]
    #       RVR_1A  MOR_1A
    # 0       3000    3000
    # 1       3000    3000
    # 2       3000    3000
    # 3       3000    3100
    # 4       3000    3100
    # ...      ...     ...
    # 5750     125      50
    # 5751     125      50
    # 5752     125      50
    # 5753     125      50
    # 5754     125      50
    #
    # [5755 rows x 2 columns]
    print(new_data)

    # .loc取某些行
    new_data = new_data.loc[20:30]
    #     RVR_1A  MOR_1A
    # 20    3000    3100
    # 21    3000    3100
    # 22    3000    3000
    # 23    3000    3000
    # 24    3000    3000
    # 25    3000    3000
    # 26    3000    3000
    # 27    3000    3100
    # 28    3000    3100
    # 29    3000    3100
    # 30    3000    3100
    print(new_data)

data = read_csv()
           
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    #取指定幾列
    new_data = data[["RVR_1A"]]
    #       RVR_1A
    # 0       3000
    # 1       3000
    # 2       3000
    # 3       3000
    # 4       3000
    # ...      ...
    # 5750     125
    # 5751     125
    # 5752     125
    # 5753     125
    # 5754     125
    #
    # [5755 rows x 1 columns]
    print(new_data)

    # .loc取某些行
    new_data = new_data.loc[20:30]
    #     RVR_1A
    # 20    3000
    # 21    3000
    # 22    3000
    # 23    3000
    # 24    3000
    # 25    3000
    # 26    3000
    # 27    3000
    # 28    3000
    # 29    3000
    # 30    3000
    print(new_data)

data = read_csv()
           
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    #取指定幾列
    new_data = data[["RVR_1A"]]
    #       RVR_1A
    # 0       3000
    # 1       3000
    # 2       3000
    # 3       3000
    # 4       3000
    # ...      ...
    # 5750     125
    # 5751     125
    # 5752     125
    # 5753     125
    # 5754     125
    #
    # [5755 rows x 1 columns]
    print(new_data)

    # .loc取某些行
    new_data = new_data.loc[0:]
    #       RVR_1A
    # 0       3000
    # 1       3000
    # 2       3000
    # 3       3000
    # 4       3000
    # ...      ...
    # 5750     125
    # 5751     125
    # 5752     125
    # 5753     125
    # 5754     125
    #
    # [5755 rows x 1 columns]
    print(new_data)

data = read_csv()
           
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    #取指定幾列
    new_data = data["RVR_1A"]
    # 0       3000
    # 1       3000
    # 2       3000
    # 3       3000
    # 4       3000
    #         ...
    # 5750     125
    # 5751     125
    # 5752     125
    # 5753     125
    # 5754     125
    # Name: RVR_1A, Length: 5755, dtype: int64
    print(new_data)

    # .loc取某些行
    new_data = new_data.loc[20:30]
    # 20    3000
    # 21    3000
    # 22    3000
    # 23    3000
    # 24    3000
    # 25    3000
    # 26    3000
    # 27    3000
    # 28    3000
    # 29    3000
    # 30    3000
    # Name: RVR_1A, dtype: int64
    print(new_data)

data = read_csv()
           
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    #取指定幾列
    new_data = data["RVR_1A"]
    # 0       3000
    # 1       3000
    # 2       3000
    # 3       3000
    # 4       3000
    #         ...
    # 5750     125
    # 5751     125
    # 5752     125
    # 5753     125
    # 5754     125
    # Name: RVR_1A, Length: 5755, dtype: int64
    print(new_data)

    # .loc取某些行
    new_data = new_data.loc[0:]
    # 0       3000
    # 1       3000
    # 2       3000
    # 3       3000
    # 4       3000
    #         ...
    # 5750     125
    # 5751     125
    # 5752     125
    # 5753     125
    # 5754     125
    # Name: RVR_1A, Length: 5755, dtype: int64
    print(new_data)

data = read_csv()
           
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

pic_path = './picture/IMG_20200823_181523.jpg'
csv_path = './csv/VIS_R06_12.csv'


def read_csv():
    data = pd.read_csv(csv_path)

    # 取指定幾列
    new_data = data[["RVR_1A", "MOR_1A"]]
    print(new_data)

    # .loc取某些行
    new_data = data.loc[0:]
    print(new_data)

    # 删除指定行
    new_data = data.drop([1])

    # # 以下兩行指令一個意思,都是删除列
    new_data = data.drop(columns="列名")
    new_data = data.drop("列名", axis=1)

    # 儲存到新的檔案.to_csv
    new_data.to_csv("./csv/new_name1.csv", index=True)  # 儲存的檔案帶列号
    new_data.to_csv("./csv/new_name2.csv", index=False)  # 儲存的檔案不帶列号


data = read_csv()