pandas绠�����浣�

�变���瑕��ㄥ��pandas澶���涓�浜�绠����版��锛�璁板�涓�涓�浣跨�ㄧ���芥�板����璺�锛�

1��pandas璇诲��csv�版��
csv��gb2312缂���锛�涔���pandas涓��存病��浠g����绀猴�涓��ラ����涔��跺��read_csv��缂����瑰�锛�浜�����open�芥�版��瀹�gb2312缂�����寮���锛�����缁�������padnas锛�杩��疯�借В�充��借В����缂�����棰���

with open("�������婚��妗�绮鹃��锛�900澶���锛�.csv","r", encoding="gb2312") as f:
    df = pd.read_csv(f)

���ュ���帮���浠ョ�存�ュ��read_csv()涓���瀹�缂����瑰�锛�

df = pd.read_csv('xxy_yang.csv', encoding="gbk")

2��pandas琛���璇诲����浣�
涓や釜涓昏��规�锛�����index绱㈠���浣�df.iloc()锛����ц�������杩�琛���浣���df.loc()
瀵逛�杩�涓�涓�涓��版��琛�锛�

import pandas as pd
data = {'浜哄��':    ['灏���', '灏�绾�', '寮�涓�', '����'],
        '�虹��骞翠唤': ['2000', '2001', '2002', '2003'],
        '楂�������':  ['630', '590', '600', '520'],
        '����':     ['5200', '3900', '4500', '3500']}
df = pd.DataFrame(data, columns=['浜哄��', '�虹��骞翠唤', '楂�������', '����', '骞寸�濂�'],
                  index=['one', 'two', 'three', 'four'])
df['骞寸�濂�'] = ['9800', '9200', '9500', '9000']
print("琛�绱㈠�锛�{}".format(list(df.index)))
print("��绱㈠�锛�{}".format(list(df.columns)))
print(df.index[1:3])
print(df.columns[1])
print(df.columns[1:3])
print(df)

杩�琛�缁���涓猴�

����index绱㈠���浣�df.iloc()渚�濡�锛�

df.iloc[1, 3]          # ��������锛�绗�浜�琛�绗�����锛�
df.iloc[[1], [3]]      # ��������锛�绗�浜�琛�绗�����锛�
df.iloc[:, 1]          # ��浣�缃���锛�浠绘��琛�绗�浜���锛�
df.iloc[:, [1, 3]]     # ��浣�缃���涓�杩�缁����版��锛�浠绘��琛�绗�浜���锛�绗�����锛�
df['浜哄��']
df.浜哄��

��琛���������浣�濡�锛�

print(df.loc['two'])
print(df.loc['two', '浜哄��'])
print(df.loc['two':'three'])
print(df.loc[['one', 'three']])
print(df.loc[['one', 'three'], ['浜哄��', '�虹��骞翠唤']])

��������锛�pandas璇诲��琛����版��-璇�缁�浠�缁�(杩�缁�&涓�杩�缁�)https://blog.csdn.net/in546/a...

3���婚��

drop_duplicates(subset=[��comment��], keep=��first��, inplace=True)

���帮�

subset锛� ��琛ㄧ��褰㈠�濉���瑕�杩�琛��婚��������锛�榛�璁や负 None 锛�琛ㄧず�规��������杩�琛���
keep锛� �������版��涓�涓�锛�first�� last�� False锛� 榛�璁ゅ�� first���朵腑锛�
锛�1锛�first 琛ㄧず锛� 淇���绗�涓�娆″�虹�扮����澶�琛�锛����ゅ���㈢����澶�琛���
锛�2锛�last 琛ㄧず锛� ���ら��澶�椤癸�淇�������涓�娆″�虹�般��
锛�3锛�False 琛ㄧず锛� ���ゆ������澶�椤广��
inplace锛�榛�璁や负 False 锛����ら��澶�椤瑰��杩���������True锛��存�ュ�ㄥ���版��涓����ら��澶�椤广��

渚�濡�锛���瑕�瀵逛��㈡�版�����烘�ョ�跺�����р���瑰�������婚��锛�

with open("浼ゅ�������瑰����涓���.csv","r", encoding="utf-8") as f:
    df = pd.read_csv(f)
df = df.iloc[:,[0,1]]
print(len(df))
df.drop_duplicates(subset=["�瑰��"],keep="first", inplace=True)
print(len(df))
print(df)

����杈��虹���锛�

��������锛�pandas�规�������婚��https://blog.csdn.net/qq_4396...

4��灏��婚�������版������csv澶���
浣跨��to_csv()锛���瀹�缂���锛����ユ�瑰���open�芥�伴��甯哥被浼笺��

5��DataFrame��query()�规�

    df = pd.read_csv("浼ゅ�������瑰����涓���_宸插�婚��.csv", encoding="utf-8")
    print(df.query("�瑰�� == '妗���姹�'"))
    print(df.query("�瑰��.str.contains('妗���姹�')"))
    fangji = '妗���姹�'
    df = df.query("�瑰��.str.startswith('{}')".format(fangji)) #绛�浠蜂�涓��㈣����
    #df = df.query("�瑰��.str.startswith(@fangji)")
    print(df)
    print(len(df.index) == 0)

你可能感兴趣的:(pandas)