为什么 df.loc[0][0] 在 python pandas 中给出第一秒而不是第一秒

why df.loc[0][0] giving first second and not the first in python pandas

一段时间后我正在研究这个项目,我很困惑,因为我不知道为什么 df.loc[0][0] 给出第二行而不是第一行。 下面是我的代码:

from datetime import datetime
import pandas as pd
#import numpy as np
import os
import time


def get_month():
    c_m = time.strftime("%b")
    return c_m
    
def get_full_month():
    c_m = time.strftime("%B")
    return c_m

def get_year():
    c_y = time.strftime("%Y")
    return c_y

def get_day():
    day = datetime.now().strftime('%A')
    return day

def get_date():
    currentdate = datetime.now().day
    return currentdate

def update_day(df):
    df.loc[0][get_date()] = get_day()

def update_date(df):
    df.loc[1][get_date()] = get_date()

def update_marks(df):
    df[str(get_date())].replace({"0": "3", "0": "3"}, inplace=True)

def create_file(df):
    df.to_csv(f"{get_month()}-{get_year()}.csv", index=False)

def update_last(df):
    thirty_day = ["April","June","September","November"]
    #print(get_month())
    if get_full_month() in thirty_day:
        print("update_last")
        if "31" in df.columns:
            df.pop("31")

def update_sunday(df):
    if get_day() == "saturday":
        df[str(int(get_date())+1)].replace({"0": "3", "0": "3"}, inplace=True)
        df.loc[0][get_date+1()] = "Sunday"


def update_late(df):

    late_teacher_numbers = input("how many teachers are late(TYPE IN NUMBERS) : ")
    if late_teacher_numbers.isnumeric:
        for f in range(int(late_teacher_numbers)):
            all_teacher = df.iloc[2:,0].tolist()
            #print(all_teacher)
            print("TEACHERS CODE LIST:\n ")
            teachers_code_list = [print(f"CODE NO   {i}   =    TEACHER NAME   {v}") for i,v in enumerate(all_teacher)]
            user = input("enter teacher code : ")
            if user.isnumeric():
                opt = input("decide between \"LATE\" \n \"V.LATE\" \n \"ABSENT\" \nenter option : ")
                opt = opt.upper()
                if opt == "LATE":
                    opt = 2
                    df.at[int(user)+2, str(get_date())] = opt
                elif opt == "V.LATE":
                    opt = 1
                    df.at[int(user)+2, str(get_date())] = opt
                elif opt == "ABSENT":
                    opt = 0
                    df.at[int(user)+2, str(get_date())] = opt

            else:
                print("PLEASE TYPE CODE FROM TEACHERS CODE LIST\n THANKYOU")
                print('PLEASE TRY AGAIN')
                main()
    else:
        print("PLEASE USE NUMERIC VALUES")
        main()

def update_csv(df):
    df.to_csv(f"{get_month()}-{get_year()}.csv", index=False)

def check_file(file):
    if os.path.isfile(file):
        return True
    else:
        return False

def main():
    file_name = f"{get_month()}-{get_year()}.csv"
    if check_file(file_name) == False:
        df = pd.read_csv("reg format.csv")
        create_file(df)
        df = pd.read_csv(file_name)
        update_date(df)
        update_day(df)
        update_last(df)
        update_marks(df)
        update_sunday(df)
        late = input("If any teacher is late \npress \"Y\" else 
 press\"N\" : ")
        if late.upper() == "Y":
            update_late(df)
            update_csv(df)
            print("ATTENDACE SHEET OF THIS MONTH : ")
            print(df)
            print("TODAYS ATTENDANCE SHEET")
            print(df[str(get_date())])
        else:
            print("ATTENDACE SHEET OF THIS MONTH : ")
            print(df)
            print("TODAYS ATTENDANCE SHEET")
            print(df[str(get_date())])
    else:
        df = pd.read_csv(file_name)
        update_last(df)
        update_date(df)
        update_day(df)
        update_marks(df)
        #update_sunday(df)
        late = input("If any teacher is late \npress \"Y\" else 
 press\"N\" : ")
        if late.upper() == "Y":
            update_late(df)
            update_csv(df)
            print("ATTENDACE SHEET OF THIS MONTH : ")
            print(df)
            print("TODAYS ATTENDANCE SHEET")
            print(df[str(get_date())])
        else:
            print("ATTENDACE SHEET OF THIS MONTH : ")
            print(df)
            print("TODAYS ATTENDANCE SHEET")
            print(df[str(get_date())])
main()

reg format file 假设

def update_day(df):
df.loc[0][get_date()] = get_day()

这会更新第二行但不会更新第一行 我想更新第一行

您可能需要检查您的 column/row 名称,因为这可能会干扰 loc. Remember that loc is a label-based indexer, while iloc 返回的基于位置的结果。

因此,对于第一行的第一个单元格,可以使用iloc,如下:

df.iloc[0, 0]

要获取整个第一行,您可以使用:

df.iloc[0, :]

或更简洁:

df.iloc[0]