1
4

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.

pandas で 最大値を抽出しその値を変更する

1
Last updated at Posted at 2020-02-29

まず、
output_filesというディレクトリ(フォルダ)に
data.csvという名前のcsvファイルをつくる。

中身は
apple, 4
banana, 5
orange, 15
となっている。

data.csv
apple, 4
banana, 5
orange, 15

このcsvファイルから、
数値が最大である15を抽出し、
その15に1を加えて16に書き換える事を目標とする。

import pandas as pd

# data.csvの最大値を+1する関数
def rewrite_pop():
    # data.csvを読み込み、最大値をpop_fur_couに格納する
    df = pd.read_csv('output_files/data.csv', names=['fru_name', 'count', ])
    pop_fur_cou = int(df.iat[df['count'].idxmax(), 1])
    # 最大値であるpop_fur_couを+1する
    pop_fur_cou += 1
    # 1増えた数値をdata.csvに上書きする
    df.iat[df['count'].idxmax(), 1] = pop_fur_cou
    df.to_csv('output_files/data.csv', index=False, header=False)

rewrite_pop()
df.iat[df['count'].idxmax(), 1] = pop_fur_cou

df.loc[df["count"].idxmax(), "count"] = pop_fur_cou

の方がいいかも。。。

1
4
1

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
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?