[英]How to use pandas to open and read text file, and find a specific piece of data?
我正在嘗試在 Jupyter Notebook 中使用 Pandas 打開一個文本文件。 然后遍歷文件,搜索並找到特定的數據。 然后將搜索到的數據行保存到一個新文件中。
我需要做什么:
我無法通過錯誤來完成代碼。 我正在努力打開文件並查找搜索數據。 請幫我。
import pandas as df
df=pd.read_csv('F:/Wells FargoZinitra.txt', 'r')
print(df)
錯誤:
從 CSV 文件讀取數據:
import pandas as pd
#df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
#put 'r' before the path string to address any special characters in the path.
df = pd.read_csv(r'F:/Wells FargoZinitra.csv')
print (df) #df is <class 'pandas.core.frame.DataFrame'>
將數據保存在 CSV 文件中:
#df.to_csv(r'Path where you want to store the CSV file\File name.csv',sep =",",index = True)
df.to_csv(r'F:/Wells FargoZinitra2.csv')
要從數據框中選擇行:
您可以查看此問題的答案並選擇適合您情況的條件。
1.) Pandas 只是在內存表中,它需要采用特定的格式。
2.) 如果它是一個平面文件(如 csv),請嘗試使用該擴展名保存它
下面是你如何做到的
import pandas as pd
df=pd.read_csv('F:/Wells FargoZinitra.csv')
print(df)
將其更改為 csv 格式或在其中使用分隔符。
此鏈接可能對您有幫助: https : //www.learnpython.org/en/Pandas_Basics
如果您是 Python 新手,我建議您從基礎開始。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.