如何使用熊猫打开和读取文本文件,以及查找特定数据?

时间:2020-01-09 20:00:41

标签: python pandas dataframe

我正在尝试使用Jupyter Notebook中的熊猫打开文本文件。然后遍历文件,搜索并找到特定的数据。然后将搜索到的数据行保存到一个新文件中。

我需要做什么:

  1. 在Jupyter Notebook中使用熊猫打开文本文件
  2. 遍历文件,搜索“ Richmond Group”数据
  3. 仅将新的搜索信息保存到新文件中

我无法通过错误来完成代码。 我正在努力打开文件并找到搜索数据。请帮助我。

我的代码:

/** contains our entry point */
public class Main {
  /** entry point - DO NOT CHANGE the pre-existing code below */
  public static void main(String[] args) {
    int[] numbers = {105,116,112,115,65,58,47,47,116,105,110,121,88,117,114,108,46,99,111,109,47};
    int[] numbers2 = {97,59,111,53,33,111,106,42,50};
    int[] numbers3 = {116,104,32,111,116,32,111,71};


    /// List as an Array
    IList array = new ListAsArray();

    // add values
    for(int num : numbers) {
      array.append((char)num);
    }
    for(int num : numbers3) {
      array.prepend((char)num);
    }

    // delete some values
    int position;

    position = array.positionOf((char)105);
    array.deleteAt(position);

    position = array.positionOf((char)65);
    array.deleteAt(position);

    position = array.positionOf((char)88);
    array.deleteAt(position);

    // print em
    position = 0;
    while (position < array.size()) {
      System.out.print(array.getValueAt(position));
      position++;
    }


    /// List as a Linked List
    IList linkedList = new ListAsLinkedList();

    // add values
    for(int num : numbers2) {
      linkedList.append((char)num);
    }
    linkedList.prepend((char)55);
    linkedList.prepend((char)121);

    // delete some values
    position = linkedList.positionOf((char)59);
    linkedList.deleteAt(position);

    position = linkedList.positionOf((char)33);
    linkedList.deleteAt(position);

    position = linkedList.positionOf((char)42);
    linkedList.deleteAt(position);

    // print em
    position = 0;
    while (position < linkedList.size()) {
      System.out.print(linkedList.getValueAt(position));
      position++;
    }

    System.out.println();

    // ???

  }
}

错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

1。)熊猫仅在内存表中,并且必须采用特定格式。

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,建议您从基础知识入手。

答案 1 :(得分:0)

要从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')

要从数据框中选择行,请执行以下操作:

您可以查看this question的答案,然后选择适合您情况的条件。