[英]Filling empty df with certain amount of values - Python
everyone!每个人! I'm trying to do something for my wife but am having some issues.我想为我的妻子做点什么,但我遇到了一些问题。 I want to create a certain value and replace info column by column.我想创建一个特定的值并逐列替换信息。
Here's what I did:这是我所做的:
import numpy as np
import pandas as pd
datalist = ['Sex', 'Race', 'Age', 'FT']
df = pd.DataFrame(np.random.randint(0,1,size=(3101, 4)), columns=datalist) #I want four columns and 3100 rows.
df = df.replace(to_replace ="0", value ="Female", limit=1752, inplace=True) #I'm trying to turn 1752 of the rows under Sex to be Female, and the rest Male.
Before I could get to the male side, I tested the df and found this:在我到达男性方面之前,我测试了 df 并发现了这个:
Sex Race Age FT
0 None 0 0 0
1 None 0 0 0
2 None 0 0 0
3 None 0 0 0
4 None 0 0 0
Why is Sex returning as none?为什么性回归没有? I've turned off the inplace but it just keeps everything as 0. What am I doing wrong?我已经关闭了就地,但它只是将所有内容都保留为 0。我做错了什么?
Thanks!谢谢!
i think loc method would be efficient to replace value(s) in a column... actually i don't know the reason why you triy to use replace method tough..我认为 loc 方法可以有效地替换列中的值...实际上我不知道您尝试使用 replace 方法强硬的原因..
df.loc[0:1752-1,'Sex']='Female'
df.loc[df.Sex!='Female',:'Sex']='Male'
print(df)
df.value_counts()
This should get you on your way, if I understand your question(s):如果我理解你的问题,这应该会让你上路:
import numpy as np
import pandas as pd
datalist = ['Sex', 'Race', 'Age', 'FT']
numpy_data = np.random.choice([0,1],size=(3101, 4))
df = pd.DataFrame(data=numpy_data, columns=datalist)
df['Sex'] = df['Sex'].astype(str)
df['Sex'].replace(to_replace ="0", value ="Female", limit=1752, inplace=True)
Simply:简单地:
df['Sex'] = ['female']*1752 + ['male']*(3101-1752)
At the end, you can shuffle your dataframe:最后,您可以洗牌您的数据框:
df.sample(frac=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.