我有一个如下所示的数据框:
In [9]: d = pd.DataFrame({'place': ['home', 'home', 'home', 'home', 'office', 'office', 'office', 'home', 'office', 'home', 'office', 'home', 'office', 'home'], 'person': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c', 'c', 'c'], 'other_stuff': ['f', 'g', 'd', 'q', 'w', 'r', 's', 't', 'u', 'v', 'w', 'l', 'm', 'n']})
In [7]: d
place other_stuff person
0 home f a
1 home g a
2 home d a
3 home q a
4 office w a
5 office r a …Run Code Online (Sandbox Code Playgroud) 我需要创建一个具有给定行数的数据框,比如说是,n并且在变量中存储一个唯一值,例如unique_value。
给定n = 6和unique_value = 25,预期的输出将是一个数据帧,该数据帧具有一个单列,6行,而在所有列中均为25:
25
25
25
25
25
25
Run Code Online (Sandbox Code Playgroud) 我有以下数据框,我想删除所有空白字符并将其设为小写:
df = pd.DataFrame({"col1":[1,2,3,4], "col2":["A","B ", "Cc","D"]})
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下方式做到这一点,df[["col2"]].apply(lambda x: x.strip().lower())但它引发了一个错误:
AttributeError: ("'Series' object has no attribute 'strip'", 'occurred at index col2')
Run Code Online (Sandbox Code Playgroud) 我知道如何使用带有填充或填充的groupby方法来估算缺少的值。但是我的问题是,我需要首先在“日期”列中找到与“得分”列中的空值最接近的日期,如果分数列中的值不为空,则用该值进行插补。如果该值为空,则需要搜索另一个最近的日期。我可以遍历行并执行此操作,但是速度非常慢。
这是数据的示例:
df = pd.DataFrame(
{'cn': [1, 1, 1, 1, 2, 2, 2],
'date': ['01/10/2017', '02/09/2016', '02/10/2016','01/20/2017', '05/15/2019', '02/10/2016', '02/10/2017'],
'score': [np.nan, np.nan, 6, 5, 4, np.nan, 8]})
cn date score
0 1 01/10/2017 NaN
1 1 02/09/2016 NaN
2 1 02/10/2016 6
3 1 01/20/2017 5
4 2 05/15/2019 4
5 2 02/10/2016 NaN
6 2 02/10/2017 8.0
Run Code Online (Sandbox Code Playgroud)
输出应该是
cn date score
0 1 01/10/2017 5
1 1 02/09/2016 6
2 1 02/10/2016 6
3 1 01/20/2017 5
4 …Run Code Online (Sandbox Code Playgroud) I am new to Pandas and was wondering if this is possible.
I have two columns one with epoch time and another with milliseconds count. I want to create a 3rd column, that has time in milliseconds using both of these as single time column that has time in ms, so that I can easily select data frame between given times. Could someone please help me with this.
my_time my_ms_counts
1500702208 1
1500702208 2
1500702208 3
1500702208 4
1500702208 5 …Run Code Online (Sandbox Code Playgroud) 我正在尝试查看是否可以在数据框中创建一个系列,该系列根据另一个系列中的单元格是否包含给定的字符串来返回单元格的值。让我解释:
我有一个包含“restaurant_name”和“brand_name”列的数据框
data = [["mcdonalds central london", ""], ["pizza hut downtown new york" ,""],
["dominos new jersey",""], ["mac donald berlin", ""]]
restaurants = pd.DataFrame(data, columns=['restaurant_name', 'brand_name'])
Run Code Online (Sandbox Code Playgroud)
我有一个字典,以字符串为键,以格式化的品牌名称为值。我希望算法检查 restaurant["restaurant_name"] 是否包含来自 brand_dictionary 的键,如果它包含我希望它返回与 data["brand_name"] 中该键对应的值
brand_dictionary = {
"mcdonalds" : "McDonald's",
"mac donald" : "McDonald's",
"dominos" : "Dominos Pizza",
"pizza hut" : "Pizza Hut"}
Run Code Online (Sandbox Code Playgroud)
真的不知道如何做到这一点..
我想将包含列表的列扩展/转换为多列:
df = pd.DataFrame({'a':[1,2], 'b':[[11,22],[33,44]]})
# I want:
pd.DataFrame({'a':[1,2], 'b1':[11,33], 'b2':[22,44]})
Run Code Online (Sandbox Code Playgroud) 我有一个简单的 pandas 数据框,包含 3 列(月份、金额、类别),其中每行代表特定类别的费用:
import pandas as pd
d = {'Month': ['Jan', 'Jan', 'Jan', 'Feb', 'Feb', 'Mar', 'Mar', 'Mar', 'Mar'], 'Amount': [5, 65, 29, 200, 28.5, 12, 4, 100, 21], 'Category': ['Travel', 'Food', 'Dentist', 'Dentist', 'Food', 'Travel', 'Food', 'Sport', 'Sport']}
df = pd.DataFrame(df)
Run Code Online (Sandbox Code Playgroud)
我想创建一个seaborn条形图,其中每个条形代表每月的支出总额,每个条形都分为不同的颜色,其中每种色调代表该月特定类别的总支出。
我能够使用相当复杂的方法并使用 matplotlib 进行绘图来实现结果:
df = df.groupby(['Month', 'Category']).sum()
df.reset_index(inplace=True)
pivot_df = df.pivot(index='Month', columns='Category', values='Amount')
df.plot.bar(stacked=True, colormap='tab20')
Run Code Online (Sandbox Code Playgroud)
但这种方法在尝试使用seaborn时会出错,而且似乎没有必要复杂。
有没有更好的方法来达到想要的结果?
我有两个pandas数据帧,如:
un do
76 0 1
32 2 3
12 0 2
56 0 1
78 2 3
6 4 4
Run Code Online (Sandbox Code Playgroud)
和
un do
76 0 5
32 2 3
12 1 2
56 0 1
78 2 3
6 4 4
34 3 3
78 h 3
23 2 -34
Run Code Online (Sandbox Code Playgroud)
因此它们代表了先前和实际数据.我需要加入所有不同的行.我即将自杀,但我不能通过pandas手段加入他们.
我想得到这样的数据帧:
un do chan
76 0 5 changed
76 0 1 None
32 2 3 None
12 1 2 changed
12 0 2 None …Run Code Online (Sandbox Code Playgroud) pandas ×9
python ×7
dataframe ×2
dictionary ×1
missing-data ×1
numpy ×1
python-3.x ×1
seaborn ×1
select ×1
string ×1