我在这里有一个代码,我想在某个csv文件中找到女性和男性的总数。

时间:2018-01-26 17:05:44

标签: python pandas csv

import pandas as pd
df = pd.read_csv('admission_data.csv')
df.head()
female = 0
male = 0
for row in df:
    if df['gender']).any()=='female':
       female = female+1             
    else:
       male = male+1

打印(女)    打印男性

CSV文件有5列Here is the picture

我想找到女性总数,男性和入学人数,入学女性人数,男性入学 谢谢。这是我尝试过的代码和上面代码的一些迭代,但它们似乎都没有用。

5 个答案:

答案 0 :(得分:3)

  1. 您的import UIKit import AVKit import AVFoundation private var looper: AVPlayerLooper? class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let path = Bundle.main.path(forResource: "Innie-Kiss", ofType:"mp4") let player = AVQueuePlayer(url: URL(fileURLWithPath: path!)) looper = AVPlayerLooper(player: player, templateItem: AVPlayerItem(asset: AVAsset(url: URL(fileURLWithPath: path!)))) let controller = AVPlayerViewController() controller.player = player let screenSize = UIScreen.main.bounds.size let videoFrame = CGRect(x: 0, y: 200, width: screenSize.width, height: (screenSize.height - 130) / 2) controller.view.frame = videoFrame self.view.addSubview(controller.view) player.play() } } 逻辑错误。

  2. 根本不需要循环。


  3. if

    或者,您可以使用print(df['gender'].tolist().count('female')) print(df['gender'].tolist().count('male')) 作为@Wen建议:

    value_counts

    经验法则:99%的时候在使用pandas时不需要使用显式循环。如果你发现自己使用了一个,那么最有可能是更好(更快)的方式。

答案 1 :(得分:3)

您只需要value_counts

df['gender'].value_counts()

答案 2 :(得分:1)

我认为您可以使用这些兄弟

// This line creates create a data frame which only have gender as male
count_male=df[df['Gender']=="male"]
// 2nd line you are basically counting how many values are there in gender column
count_male['Gender'].size

(或)

count_male=df['Gender']=="male"]
count_male.sum()

答案 3 :(得分:0)

我创建了以下csv文件:

student_id,gender,major,admitted
35377,female,chemistry,False
56105,male,physics,True
31441,female,chemistry,False
51765,male,physics,True
31442,female,chemistry,True

将csv文件读入dataframe:

import pandas as pd
df=pd.read_csv('D:/path/test1.csv', sep=',')
df[df['admitted']==True].groupby(['gender','admitted']).size().reset_index(name='count')

df
    gender  admitted    count
0   female  True    1
1   male    True    2

希望这有帮助!

答案 4 :(得分:-2)

获取列gender中的值,存储在列表中,并计算出现次数:

import pandas as pd
df = pd.read_csv('admission_data.csv')

print(list(df['gender']).count('female'))
print(list(df['gender']).count('male'))