Python:在Pandas中查找数据框中位置的值,知道一列中的值

时间:2017-12-04 17:15:41

标签: python pandas dataframe

我在python中有一个pandas数据框:

              Date    Open    High     Low   Close     Volume  Market Cap  \
0     Nov 21, 2017  136.10  142.21  131.37  141.74   72701500  2093010000   
1     Nov 20, 2017  129.50  140.55  128.49  135.76   59945400  1990900000   
2     Nov 19, 2017  130.72  133.62  127.21  129.48   42133000  2009180000   
3     Nov 18, 2017  126.59  135.01  123.53  130.80   62156400  1945200000   
4     Nov 17, 2017  120.02  126.70  119.25  126.70   47127700  1843600000   
5     Nov 16, 2017  121.24  128.09  118.50  120.31   74944100  1861940000   
6     Nov 15, 2017  122.24  124.72  119.19  121.37   49976700  1876740000   
7     Nov 14, 2017  123.62  124.20  118.63  122.35   53544800  1896990000   
8     Nov 13, 2017  128.96  136.53  120.92  123.40  116200000  1978840000   
9     Nov 12, 2017  119.60  133.68  110.62  123.86  144948000  1834630000   
10    Nov 11, 2017  105.75  127.11  103.88  119.62  107708000  1621800000   

这存储为test.history。我需要获取日期Close的列20 of November 2017的值。我该怎么做?

我试过了:

print testcoin.history.loc['Date'=='Nov 20, 2017','Close']

print testcoin.history.loc('Nov 20, 2017','Close')

还有很多其他可能性,但我显然不了解数据帧。

P.S。我试着在这里搜索这个答案,并在网上和教程中寻找答案,但我似乎总是缺少一些基本元素来使它工作。

1 个答案:

答案 0 :(得分:0)

您可以尝试:

export class App implements OnInit {
  private imageUrls: string[];
  private current: number = 0;
  currentImage: string;
  constructor() {
    this.imageUrls = [
      '//placehold.it/1280x720.jpg',
      '//placehold.it/1280x721.jpg',
      '//placehold.it/1280x722.jpg',
      '//placehold.it/1280x723.jpg'
    ];
    this.currentImage = this.imageUrls[0]
  }
  ngOnInit() {

    // Every second...
    Observable.interval(1000)
    .subscribe(x => {
      this.currentImage = `url(${this.imageUrls[this.current]})`;

      // Reset current to 0 if we're at the end of the array
      this.current == this.imageUrls.length - 1 ? (this.current = 0) : ++this.current;
    })
  }
}

如果您的数据框被称为tescoin.history,请在代码中用df替换该名称