pandas 显示输出的原始 Dataframe 和编辑

wlzqhblo  于 2023-11-15  发布在  其他
关注(0)|答案(3)|浏览(92)

它的新手在这里再次.我想问你一次,如何显示原始输出,然后再显示修改或编辑输出.这是我的代码

import pandas as pd

source = r'C:\Users\pulun\hbrag08\Durga Software\Complete Python Package\Python for Data Science\04. Pandas\DataFrame\employee_data.csv'
df = pd.read_csv(source,
                   usecols=['eno','ename','esal','city','country'],
                   index_col='eno',
                   sep=';')

df.rename(columns={'country': 'country name'},inplace=True)

print(df.sort_values(['esal']).loc[df['country name'].isin(
    ['Indonesia', 'USSR'])].head(n=6))

df.loc[(df['esal'] <= 1300000) & (df['country name'].isin(
    ['Indonesia', 'USSR']))] = df.loc[(df['esal'] <= 1300000) & (df['country name'].isin(
        ['Indonesia', 'USSR']))].apply(lambda x: x*2)

print('your solution here')

字符串
原始输出如下

ename        esal                    city country name
eno
2840       Duffy Stood  1039768.45    9488 Milwaukee Plaza         USSR
5590     Sawyere Clogg  1096587.82  3409 Rockefeller Alley         USSR
730       Garret Attew  1104028.84          7 Walton Point         USSR
9040    Marve Coalburn  1207724.68          97 Susan Point         USSR
1620   Chrotoem Cleeve  1280881.36                     NaN         USSR
9500  Eleanore Cabrera  1294722.19          2401 Hovde Way    Indonesia


我想要的是类似的输出以上,但与esal是一倍。
after输出必须看起来像下面这样:

ename        esal                    city country name
eno
2840       Duffy Stood  2079536.90   9488 Milwaukee Plaza         USSR
5590     Sawyere Clogg  2193175.64  3409 Rockefeller Alley         USSR
730       Garret Attew  2208057.68          7 Walton Point         USSR
9040    Marve Coalburn  1207724.68          97 Susan Point         USSR
1620   Chrotoem Cleeve  2415449.36                     NaN         USSR
9500  Eleanore Cabrera  2589444.38          2401 Hovde Way    Indonesia


对于你的帮助,我表示非常感谢

7gyucuyw

7gyucuyw1#

试试看:

import pandas as pd

source = r'C:\Users\pulun\hbrag08\Durga Software\Complete Python Package\Python for Data Science\04. Pandas\DataFrame\employee_data.csv'
df = pd.read_csv(source,
                   usecols=['eno','ename','esal','city','country'],
                   index_col='eno',
                   sep=';')

df.rename(columns={'country': 'country name'},inplace=True)

print(df.sort_values(['esal']).loc[df['country name'].isin(
    ['Indonesia', 'USSR'])].head(n=6))

df = df[(df['esal'] <= 1300000) & (df['country name'].isin(
        ['Indonesia', 'USSR']))]
df['esal'] = df['esal']*2

print(df)

字符串

k75qkfdt

k75qkfdt2#

尝试替换:

df.loc[(df['esal'] <= 1300000) & (df['country name'].isin(
    ['Indonesia', 'USSR']))] = df.loc[(df['esal'] <= 1300000) & (df['country name'].isin(
        ['Indonesia', 'USSR']))].apply(lambda x: x*2)

字符串
这几行代码:

import numpy as np
df['esal'] = np.where((df['esal'] <= 1300000) & (df['country name'].isin(['Indonesia', 'USSR'])), df['esal']*2, df['esal'])

guicsvcw

guicsvcw3#

只需创建一个名为esal_orig的新列,您就可以比较前后。

import pandas as pd
import numpy as np

df['esal_orig'] = df['esal']

mask = ( (df['esal'] <= 1300000) &
         (df['country name'].isin(['Indonesia', 'USSR']))
       )

df['esal'] = np.where(mask, df['esal']*2, df['esal'])

print(df[['esal_orig', 'esal']])

个字符

相关问题