0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

あるデータの要素で変数(リスト)を分けたい場合

Last updated at Posted at 2018-12-01

あるデータの要素で変数(リスト)を分けたい場合の方法です。
例として、下記のようなファイル(test.csv)を読み込み、"SAMPLE"ごとにリストに分けて格納していく方法です。今回は"SAMPLE"が1と2なので、2つに分けます。

test.csv

testcsv.png

プログラム
test.py
import pandas as pd

# ファイル情報を読み込む
test_info=pd.read_csv("test.csv")

# SAMPLE数の最大値をサーチし格納する。今回は2。
sample_count=test_info["SAMPLE"].max()

# SAMPLEごとに格納するリストの変数宣言
sample_data=[]


# SAMPLEごとにsample_dataのリストに格納する。
for i in range(sample_count):
    j=i+1
    sample_data.append(test_info[test_info["SAMPLE"]==j])
結果
sample_data[0]

sampledata14.png

sample_data[1]

sampledata2.png

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?