【问题标题】:How to append something in a CSV file to a column in all the rows where the cell of the ticker column = 'AAPL'?如何将 CSV 文件中的内容附加到代码列的单元格 = 'AAPL' 的所有行中的列?
【发布时间】:2021-01-17 13:12:07
【问题描述】:

快速提问:我正在尝试对 CSV 文件中的代码进行一些分析。

CSV 文件示例(请注意,这些只是前两行,总共有大约 200 个代码):

,ticker,dimension,calendardate,datekey,reportperiod,lastupdated,accoci,assets,assetsavg,assetsc,assetsnc,assetturnover,bvps,capex,cashneq,cashnequsd,cor,consolinc,currentratio,de,debt,debtc,debtnc,debtusd,deferredrev,depamor,deposits,divyield,dps,ebit,ebitda,ebitdamargin,ebitdausd,ebitusd,ebt,eps,epsdil,epsusd,equity,equityavg,equityusd,ev,evebit,evebitda,fcf,fcfps,fxusd,gp,grossmargin,intangibles,intexp,invcap,invcapavg,inventory,investments,investmentsc,investmentsnc,liabilities,liabilitiesc,liabilitiesnc,marketcap,ncf,ncfbus,ncfcommon,ncfdebt,ncfdiv,ncff,ncfi,ncfinv,ncfo,ncfx,netinc,netinccmn,netinccmnusd,netincdis,netincnci,netmargin,opex,opinc,payables,payoutratio,pb,pe,pe1,ppnenet,prefdivis,price,ps,ps1,receivables,retearn,revenue,revenueusd,rnd,roa,roe,roic,ros,sbcomp,sgna,sharefactor,sharesbas,shareswa,shareswadil,sps,tangibles,taxassets,taxexp,taxliabilities,tbvps,workingcapital,sector
4327,AAPL,ARQ,2007-06-30,2007-08-08,2007-06-30,2020-08-31,56000000.0,21647000000.0,,18745000000.0,2902000000.0,,0.552,-283000000.0,7118000000.0,7118000000.0,3415000000.0,818000000.0,2.681,0.615,0.0,0.0,0.0,0.0,0.0,81000000.0,0.0,0.0,0.0,1196000000.0,1277000000.0,0.23600000000000002,1277000000.0,1196000000.0,1196000000.0,0.034,0.033,0.034,13404000000.0,,13404000000.0,109420845098.0,24.0,22.848000000000003,944000000.0,0.039,1.0,1995000000.0,0.369,275000000.0,0.0,7262000000.0,,251000000.0,6649000000.0,6649000000.0,0.0,8243000000.0,6992000000.0,1251000000.0,116538845098.0,23000000.0,-6000000.0,118000000.0,0.0,0.0,229000000.0,-1433000000.0,-1170000000.0,1227000000.0,0.0,818000000.0,818000000.0,818000000.0,0.0,0.0,0.151,954000000.0,1041000000.0,3660000000.0,0.0,8.693999999999999,37.185,36.815,1626000000.0,0.0,4.7860000000000005,5.151,5.1339999999999995,1410000000.0,8199000000.0,5410000000.0,5410000000.0,208000000.0,,,,,65000000.0,746000000.0,1.0,24349946740.0,24270568000.0,24938788000.0,0.223,21372000000.0,687000000.0,378000000.0,0.0,0.8809999999999999,11753000000.0
4328,AAPL,ARQ,2007-09-30,2007-11-15,2007-09-29,2020-08-31,63000000.0,25347000000.0,,21956000000.0,3391000000.0,,0.596,-205000000.0,9352000000.0,9352000000.0,4127000000.0,904000000.0,2.3609999999999998,0.7440000000000001,0.0,0.0,0.0,0.0,0.0,93000000.0,0.0,0.0,0.0,1230000000.0,1323000000.0,0.213,1323000000.0,1230000000.0,1230000000.0,0.037000000000000005,0.036000000000000004,0.037000000000000005,14532000000.0,,14532000000.0,134496640397.0,27.0,25.258000000000003,1491000000.0,0.061,1.0,2090000000.0,0.336,337000000.0,0.0,6359000000.0,,346000000.0,6034000000.0,6034000000.0,0.0,10815000000.0,9299000000.0,1516000000.0,143848640397.0,2234000000.0,-29000000.0,68000000.0,0.0,0.0,142000000.0,396000000.0,615000000.0,1696000000.0,0.0,904000000.0,904000000.0,904000000.0,0.0,0.0,0.145,1030000000.0,1060000000.0,4970000000.0,0.0,9.899,41.147,40.748000000000005,1832000000.0,0.0,5.867999999999999,5.992000000000001,5.917000000000001,1637000000.0,9101000000.0,6217000000.0,6217000000.0,207000000.0,,,,,68000000.0,823000000.0,1.0,24515127672.0,24384640000.0,25084724000.0,0.255,25010000000.0,782000000.0,326000000.0,0.0,1.026,12657000000.0

我希望能够为“ticker”列=“AAPL”的所有行,向“sector”列写入一些内容。然后对于“ticker”列=“GOOGL”的所有列,在“sector”列中写入一些内容。我该怎么做?

我会展示我的代码,但它确实没有多大帮助。

【问题讨论】:

标签: python python-3.x pandas dataframe


【解决方案1】:

如果您想根据条件设置某些列值,请考虑 applyiterrows

def set_vals(row):
  result = ''
  if row['ticker'] == 'AAPL':
    result = 'something1'
  elif row['ticker'] == 'GOOGL':
    result = 'something2'
  return result

df['sector'] = df.apply(set_vals,axis=1)
df

编辑:

如果不想使用迭代逻辑,条件简单的话也可以使用

df.loc[df['ticker'] == 'AAPL', 'sector'] = 'something1' 
df.loc[df['ticker'] == 'GOOGL', 'sector'] = 'something2'

df

【讨论】:

  • @Parfait 是的先生,我已经相应地更新了我的答案。如果您有任何其他选择,请告诉我,因为它也会帮助我。我也是新手:)
【解决方案2】:

考虑使用DataFrame.locnumpy.wherenumpy.select 进行条件逻辑赋值:

df.loc[df['ticker'] == 'AAPL', 'sector'] = 'myvalue'
df['Sector'] = np.where(df['ticker'].eq('AAPL'), 
                        'myvalue', 
                        df['sector'])
conds = [df['ticker'].eq('AAPL'), 
         df['ticker'].eq('GOOGL')
         ...]

vals = ['my value for AAPL',
        'my value for GOOGL',
        ...]

df['sector'] = np.select(conds, vals, default=df['sector'])

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2014-05-13
    • 2023-03-25
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多