首页 > 解决方案 > 如何从 pandas read_csv 获取值/行

问题描述

我正在使用包含有关加密新闻的 CSV 文件。我的目标是练习字符串操作和方法。CSV 看起来像这样:

publishdate headlinetext
20130504    COnSTELlATIon DaG iS nOW liStEd On kucoiN eXC?haNGE
20130511    ItA*lys cRypTOCUrREnCy BITgrAil suspeNds OpERatIOnS
20130511    THe diffeRENCe bETWEEn sHarEs aNd cRYpToCUrReN€CiES
20130512    fedS seIzE 47 mIlLION In bItCoinS in FAke ID ST=ing
20130514    ThE diG sTarteD ASiCboOST neTwORK AnD b@ItcoIN cAsH
20130516    BINAncE far atualiZAO progRaMadA NEsTa QuarTAFEIR?a
20130516    tHe EUropeaN UniOn IS pLaNninG tO rEgULAtE Bi=TcOIn
20130516    i!BeROBiT HELpiNG bItcOIn To GO MAinStream IN sPaIN
20130521    EuropES sMALLEr €banKS WELComE CrypTOcuRRENCy uSerS
20130604    BiTcoIn btc hIghER bTc= price BRiNgs mOrE Btc SCAmS
20130610    BITCOin brEAkS 9000 iN latEST LANDmARK Pr#iCE pOinT
20130613    ubcoiN mArkEt movEs ItS HEAdQUArTERS To €SiNgaPorE 
20130624    reeds jEwelErs TaKinG bitCOiN ONLiNe And IN Sto$Res
20130705    CoNtrOvE!RSY turnS to cLoSuRe as LItePAy SHUts DowN
20130709    bUll rESiSTAncE BITCOIn pRicE nEeDs brEAK AbOve 9K*
20130714    DIVoRcE DISpUte co#Uple fIghtS ovEr 830k Of BITcoin
20130718    10K agaIn For BitcoiN buT oT!her CRyptOs OUTperfORM
20130724    FACebOoKS liBRa crYptoCUrrency wHER$E aRE ThE BANkS
20130726    COULd eNjIn coiN ReacH A neW AlltiM=E hIGh in APRIL
20130827    the GReaT Tug of WaR betWE=eN bItCOiNS anD AltCOINs
20130827    The SacRAMento kINgs mINE EthEreuM ETh for C#HArItY
20130905    cryPtOCuRREncY aTMs tHE KEY T*o WIdeSPRead ADoPtiOn
20130909    GraySCales EtHereUM TRusT pRICE= VaLUeS Eth aT 6000
(...)

然后我用 pandas 来读取 CSV。


import pandas as pd

news_headlines = pd.read_csv('/content/sample_data/crypto_headlines.csv')

news_headlines

现在我需要让字符串与它们一起使用并将它们更改为小写或大写,然后删除特殊字符。

但是,我不知道应该使用哪种方法从我创建的名为 news_headlines 的变量中提取字符串。

假设我想提取第二行,发布日期为 20130511。

有什么帮助吗?

提前致谢

标签: pythonpandas

解决方案


您可以使用iloc(),如下例所示,提取与第二行相关的字符串:

news_headlines.iloc[1, 1]

推荐阅读