简体   繁体   English

Pandas:在列中查找最小值,将包含该列的行写入新数据帧

[英]Pandas: Find minimum value in a column, write the row containing that column to a new dataframe

I have a large number of simple time series in unique CSV files. 我在独特的CSV文件中有大量简单的时间序列。 Each file contains a "Date" column and "Close" column. 每个文件都包含“日期”列和“关闭”列。

I would like to use pandas to read the data for each file into a data frame, find the minimum value in the "Close" column, and write both the minimum "Close" value and associated "Date" to a new dataframe. 我想使用pandas将每个文件的数据读入数据框,在“关闭”列中找到最小值,并将最小“关闭”值和关联的“日期”写入新数据帧。

This would ideally produce a new dataframe that contains minimum "Close" values and the date on which that minimum occurred, for all files screened. 理想情况下,对于筛选的所有文件,这将生成一个新的数据框,其中包含最小“关闭”值和最小值发生的日期。

import pandas as pd
import os

symbol = "LN"
start_year = 2010
end_year = 2014
months = ["G", "J", "M", "N", "Q", "V", "Z"]

def historiclows():
    df1 = pd.read_csv("%s.csv" % (file3))
    df1 = df1.drop(df1.columns[[1,2,3,5,6]], axis = 1)
    targetvalues = df1.loc[df1["Close"].idxmin()]
    df2.append(targetvalues)

for m in months:
df2 = pd.DataFrame()    

for y in range(start_year, end_year+1):
    if m != "Z":
        if months[months.index(m)+1] != "Z":
            file1 = ("%s%s%s%s%s%s" % (symbol, m, y, symbol, months[months.index(m)+1], y))
            file2 = ("%s%s%s%s%s%s" % (symbol, months[months.index(m)+1], y, symbol, months[months.index(m)+2], y))
            file3 = ("%s%s" % (file1, file2))
            checkfile3 = os.path.isfile("%s.csv" % file3)
            if checkfile3 == True:
                title = ("%s%s%s" % (m, months[months.index(m)+1], months[months.index(m)+2]))
                historiclows()
                print(df2)

            else:
                pass

        else:
            file1 = ("%s%s%s%s%s%s" % (symbol, m, y, symbol, months[months.index(m)+1], y))
            file2 = ("%s%s%s%s%s%s" % (symbol, months[months.index(m)+1], y, symbol, str(months[0]), y+1))
            file3 = ("%s%s" % (file1, file2))
            checkfile3 = os.path.isfile("%s.csv" % file3)
            if checkfile3 == True:
                title = ("%s%s%s" % (m, months[months.index(m)+1], str(months[0])))
                historiclows()
                print(df2)

            else:
                pass

    else:
        file1 = ("%s%s%s%s%s%s" % (symbol, m, y, symbol, str(months[0]), y+1))
        file2 = ("%s%s%s%s%s%s" % (symbol, str(months[0]), y+1, symbol, str(months[1]), y+1))
        file3 = ("%s%s" % (file1, file2))
        checkfile3 = os.path.isfile("%s.csv" % file3)
        if checkfile3 == True:
            title = ("%s%s%s" % (m, str(months[0]), str(months[1])))
            historiclows()
            print(df2)

        else:
            pass

print("!!! PROCESS COMPLETE !!!") print(“!!! PROCESS COMPLETE !!!”)

You can simply do: 你可以简单地做:

>> orig_df
            Close
2015-01-01      4
2015-02-01      1
2015-03-01      3
2015-03-01      1

new_df = orig_df[orig_df['Close'] == min(orig_df['Close'])]

>> new_df
            Close
2015-02-01      1
2015-03-01      1

Then if you only want the minimum to appear once in the new dataframe you can use drop_duplicates : 然后,如果您只希望最小值在新数据帧中出现一次,则可以使用drop_duplicates

new_df.drop_duplicates(subset=['Close'], inplace=True)

>>          Close
2015-02-01      1

If you want the last date and not the first date, do 如果您想要最后一个日期而不是第一个日期,请执行

new_df.drop_duplicates(subset=['Close'], inplace=True, take_last=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Pandas dataframe 中找到最小值并在新列上添加 label - Find the minimum value in a Pandas dataframe and add a label on new column 在整个Pandas DataFrame中获取具有最小值的行和列 - Get Row and Column with Minimum value in Entire Pandas DataFrame Select a pandas dataframe 行,其中列具有最小值 - Select a pandas dataframe row where column has minimum value Pandas:如何根据每行包含 json 的列值创建新的 dataframe? - Pandas: how to create a new dataframe depending on a column value containing json for each row? 在 dataframe 中查找值并在 pandas 的新列中添加先例列值 - find a value in a dataframe and add precedent column value in a new column in pandas 如何评估 pandas dataframe 中一行的所有值并写入新列 - How to assess all values of a row in a pandas dataframe and write into a new column (行、列):值到 Pandas DataFrame - (Row, Column) : Value to Pandas DataFrame 如何将函数的返回值写入pandas数据框的新列 - How to write a return value of a function into new column of a pandas dataframe 如果多列中的行包含 1 如何在 dataframe 中添加新列,其中包含值为 1 的列名 - if row in multiple column contains 1 how to add new column in dataframe containing column names where value is 1 如何在数据框中拆分一列并将每个值存储为新行(以熊猫为单位)? - How to split a column in a dataframe and store each value as a new row (in pandas)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM