Python 如何将变量组织到dataframe中,但不填充dataframe每列的变量值?
我想将变量中的数据添加到数据帧中。我遇到的问题是,我需要组织变量,使它们不会填充每一行 我需要这样的数据:Python 如何将变量组织到dataframe中,但不填充dataframe每列的变量值?,python,pandas,dataframe,Python,Pandas,Dataframe,我想将变量中的数据添加到数据帧中。我遇到的问题是,我需要组织变量,使它们不会填充每一行 我需要这样的数据: name notification1 notification2 notification3 a 1 b 2 c 3 name notification1 notific
name notification1 notification2 notification3
a 1
b 2
c 3
name notification1 notification2 notification3
a 1 1 1
b 2 2 2
c 3 3 3
数据帧当前看起来如下所示:
name notification1 notification2 notification3
a 1
b 2
c 3
name notification1 notification2 notification3
a 1 1 1
b 2 2 2
c 3 3 3
变量设置如下(所有变量均为str):
每个通知只有一个人附加到它,所以不是每一行都需要每个人的数据
提前谢谢,希望我的问题有意义 考虑将数据存储在列表中,而不是单个变量中
notifs = [1, 2, 3]
persons = ['a', 'b', 'c']
使用np.diag
初始化对角线2D数组,并将其传递给pd.DataFrame
:
pd.DataFrame(
np.diag(notifs),
index=persons,
columns=np.arange(1, len(notifs) + 1)
).add_prefix('notification')
notification1 notification2 notification3
a 1 0 0
b 0 2 0
c 0 0 3
考虑将数据存储在列表中,而不是单个变量中
notifs = [1, 2, 3]
persons = ['a', 'b', 'c']
使用np.diag
初始化对角线2D数组,并将其传递给pd.DataFrame
:
pd.DataFrame(
np.diag(notifs),
index=persons,
columns=np.arange(1, len(notifs) + 1)
).add_prefix('notification')
notification1 notification2 notification3
a 1 0 0
b 0 2 0
c 0 0 3
一种方法是:
import pandas as pd
notification1 = 1.0
notification2 = 2.0
notification3 = 3.0
person_notification1 = 'a'
person_notification2 = 'b'
person_notification3 = 'c'
def row(name, notification):
return {'name': name, 'notification_'+str(notification) : notification}
df = pd.DataFrame()
df = df.append(row(person_notification1, int(notification1)),ignore_index=True)
df = df.append(row(person_notification2, int(notification2)),ignore_index=True)
df = df.append(row(person_notification3, int(notification3)),ignore_index=True)
结果是:
name notification_1 notification_2 notification_3
0 a 1.0 NaN NaN
1 b NaN 2.0 NaN
2 c NaN NaN 3.0
一种方法是:
import pandas as pd
notification1 = 1.0
notification2 = 2.0
notification3 = 3.0
person_notification1 = 'a'
person_notification2 = 'b'
person_notification3 = 'c'
def row(name, notification):
return {'name': name, 'notification_'+str(notification) : notification}
df = pd.DataFrame()
df = df.append(row(person_notification1, int(notification1)),ignore_index=True)
df = df.append(row(person_notification2, int(notification2)),ignore_index=True)
df = df.append(row(person_notification3, int(notification3)),ignore_index=True)
结果是:
name notification_1 notification_2 notification_3
0 a 1.0 NaN NaN
1 b NaN 2.0 NaN
2 c NaN NaN 3.0
我认为您希望每个列只有一个值,其余为空字符串。请在下面找到我的解决方案。我希望有帮助
import pandas as pd
import numpy as np
def main():
notification = [1.0, 2.0, 3.0]
persons = ['a', 'b', 'c']
columns = ['notification{}'.format(i) for i, elem in enumerate(notification, 1)]
df = pd.DataFrame(columns=columns)
for r,c,v in zip(persons,columns,notification):
df.at[r,c] = v
df = df.replace(np.nan, '', regex=True)
print(df)
if __name__ == '__main__':
main()
输出
notification1 notification2 notification3
a 1
b 2
c 3
我认为您希望每个列只有一个值,其余为空字符串。请在下面找到我的解决方案。我希望有帮助
import pandas as pd
import numpy as np
def main():
notification = [1.0, 2.0, 3.0]
persons = ['a', 'b', 'c']
columns = ['notification{}'.format(i) for i, elem in enumerate(notification, 1)]
df = pd.DataFrame(columns=columns)
for r,c,v in zip(persons,columns,notification):
df.at[r,c] = v
df = df.replace(np.nan, '', regex=True)
print(df)
if __name__ == '__main__':
main()
输出
notification1 notification2 notification3
a 1
b 2
c 3
用于创建此数据帧的代码在哪里?用于创建此数据帧的代码在哪里?